Search code examples
cssabcpdf

ABCpdf not rendering images or applying CSS


The problem is that the CSS is not applying and the images are not rendering in the PDF. This only happens on a staging and production environment. Unable to recreate the problem on my development environment.

Both are under https, the links to the images and CSS files are http. So, I changed my development environment to use https to see if this was the issue. It didn't make any difference.

This is the code I have in place for rendering the PDF from HTML (I didn't write it, its quite old code):

Doc theDoc = new Doc();
theDoc.HtmlOptions.Engine = EngineType.Gecko;
theDoc.HtmlOptions.UseScript = true;
theDoc.HtmlOptions.Media = MediaType.Screen;
theDoc.HtmlOptions.AutoTruncate = true;

XHtmlOptions.GeckoSubsetType options = theDoc.HtmlOptions.GeckoSubset;
options.AddLinks = true;

//Write the CSS to PDF
StringBuilder coreCssBuilder = new StringBuilder();
StringWriter coreCssWriter = new StringWriter(coreCssBuilder);
HtmlTextWriter coreCssHtmlWriter = new HtmlTextWriter(coreCssWriter);
lnkCoreCss.RenderControl(coreCssHtmlWriter);

String coreCssHtml = coreCssBuilder.ToString().Replace("href=\"..", "href=\"http://example.com");

StringBuilder testTranscriptCssBuilder = new StringBuilder();
StringWriter testTranscriptCssWriter = new StringWriter(testTranscriptCssBuilder);
HtmlTextWriter testTranscriptCssHtmlWriter = new HtmlTextWriter(testTranscriptCssWriter);
lnkTestTranscriptCss.RenderControl(testTranscriptCssHtmlWriter);

String testTranscriptCssHtml = testTranscriptCssBuilder.ToString().Replace("href=\"..", "href=\"http://example.com");

StringBuilder templateTableCssBuilder = new StringBuilder();
StringWriter templateTableCssWriter = new StringWriter(templateTableCssBuilder);
HtmlTextWriter templateTableCssHtmlWriter = new HtmlTextWriter(templateTableCssWriter);
lnkTemplateTableCss.RenderControl(templateTableCssHtmlWriter);

String templateTableCssHtml = templateTableCssBuilder.ToString().Replace("href=\"..", "href=\"http://example.com");

StringBuilder extraCssBuilder = new StringBuilder();
StringWriter extraCssWriter = new StringWriter(extraCssBuilder);
HtmlTextWriter extraCssHtmlWriter = new HtmlTextWriter(extraCssWriter);
styleExtraCss.RenderControl(extraCssHtmlWriter);

String extraCssHtml = extraCssBuilder.ToString().Replace("</style>", ".BorderCell {border: none;}</style>");

StringBuilder transcriptBuilder = new StringBuilder();
StringWriter transcriptWriter = new StringWriter(transcriptBuilder);
HtmlTextWriter transcriptHtmlWriter = new HtmlTextWriter(transcriptWriter);

pnlTranscript.RenderControl(transcriptHtmlWriter);

//Add the transcript html to the PDF
String transcriptHtml = transcriptBuilder.ToString()
    .Replace("src=\"/", "src=\"http://example.com/")
    .Replace("src=\"../", "src=\"http://example.com/");

//Add the page CSS to the PDF
transcriptHtml = coreCssHtml + testTranscriptCssHtml + templateTableCssHtml + extraCssHtml + transcriptHtml;

int pageId = theDoc.AddImageHtml(transcriptHtml);

while (true)
{
    if (!theDoc.Chainable(pageId))
        break;
    theDoc.Page = theDoc.AddPage();
    pageId = theDoc.AddImageToChain(pageId);
}

//Flatten pages
for (int i = 1; i <= theDoc.PageCount; i++)
{
    theDoc.PageNumber = i;
    theDoc.Flatten();
}

return theDoc;

More information

  • ABCpdf .NET version 8.1 x64
  • Staging and production environments run IIS 8.5 on Windows Server 2012 R2
  • Development environment runs IIS 7.5

Can anyone help with this?


Solution

  • It turns out that the site where the CSS and images were stored did not have the correct DNS settings in order to allow my staging and production environments to access it.

    Adjusted the DNS settings and this now works.