How can I use iText to convert HTML with images and hyperlinks to PDF?
<p>I'm trying to convert <code>HTML</code> to <code>PDF</code> using iTextSharp in an <code>ASP.NET</code> web application that uses both <a href="http://www.asp.net/mvc" rel="nofollow">MVC</a>, <strong>and</strong> <a href="http://www.asp.net/web-forms" rel="nofollow">web forms</a>. The <code>&lt;img&gt;</code> and <code>&lt;a&gt;</code> elements have absolute and <strong>relative</strong> URLs, and some of the <code>&lt;img&gt;</code> elements are <a href="https://en.wikipedia.org/wiki/Data_URI_scheme" rel="nofollow"><strong>base64</strong></a>. Typical answers here at SO and Google search results use generic <code>HTML</code> to <code>PDF</code> code with <code>XMLWorkerHelper</code> that looks something like this:</p>

<pre><code>using (var stringReader = new StringReader(xHtml))
{
    using (Document document = new Document())
    {
        PdfWriter writer = PdfWriter.GetInstance(document, stream);
        document.Open();
        XMLWorkerHelper.GetInstance().ParseXHtml(
            writer, document, stringReader
        );
    }
}
</code></pre>

<p>So with sample <code>HTML</code> like this:</p>

<pre><code>&lt;div&gt;
    &lt;h3&gt;HTML Works, but Broken in Converted PDF&lt;/h3&gt;
    &lt;div&gt;Relative local &lt;img&gt;: &lt;img src='./../content/images/kuujinbo_320-30.gif' /&gt;&lt;/div&gt;
    &lt;div&gt;
        Base64 &lt;img&gt;:
        &lt;img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' /&gt;
    &lt;/div&gt;
    &lt;div&gt;&lt;a href='/somePage.html'&gt;Relative local hyperlink, broken in PDF&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
</code></pre>

<p>The resulting PDF: <strong>(1)</strong> is missing all images, and <strong>(2)</strong> all hyperlink(s) with relative URLs are broken and use a <a href="https://en.wikipedia.org/wiki/File_URI_scheme" rel="nofollow">file URI scheme</a> (<code>file///XXX...</code>) instead of pointing to the correct web site.</p>

<p>Some answers here at SO and others from Google search recommend replacing relative URLs with absolute URLs, which is <em>perfectly acceptable</em> for one-off cases. However, globally replacing all <code>&lt;img src&gt;</code> and <code>&lt;a href&gt;</code> attributes with a hard-coded string is <strong>unacceptable</strong> for this question, so please do not post an answer like that, because it will accordingly be downvoted. </p>

<p>Am looking for a solution that works for <strong>many</strong> different web applications residing in test, development, and production environments.</p>