IronPDF CSS is not working when rendering a PDF

258 Views Asked by At

we updated a 4.6.1 app to windows-net6 and now the CSS Rendering for PDF's aren't working. When rendering the view as HTML, the css and outputted PDF looks fine. Anybody have any issues like this before?

We were running a 2019 version of IRON PDF. I quickly updated that to the most recent version and I was still having issues.

From the view, when I update the the source url, it works.

Like this. But that isn't feasible in the long run. Well, I guess we can update all the views with a variable, but it would be annoying.
<link href="@Url.Content("http:localhost:8888/content/font-awesome.css")" rel="stylesheet" type="text/css" />

Example code.

  var htmlPage = RenderRazorViewToString("SpanishBenefitSummaryForHTML", planModel).Result;
  var htmlHeader = RenderRazorViewToString("SpanishBenHeader", planModel).Result;
  var htmlFooter = RenderRazorViewToString("SpanishBenFooter", planModel).Result;
  var Renderer = new ChromePdfRenderer();
  Renderer.RenderingOptions.MarginTop = 60;  //millimeters
  Renderer.RenderingOptions.MarginBottom = 10;
  Renderer.RenderingOptions.MarginLeft = 3;
  Renderer.RenderingOptions.MarginRight = 3;
  Renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter() { HtmlFragment = htmlHeader };
  Renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter() { HtmlFragment = htmlFooter };
  var PDF = Renderer.RenderHtmlAsPdf(htmlPage, _webHostEnvironment.ContentRootPath + @"wwwroot\Content\");

  if (planModel.Status != "Active")
  {
      PDF.ApplyWatermark("  <div style='font-size:10em;font-family: Verdana;font-weight: bold'>  DRAFT </ div > ", 30,
      IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center);
  }

  string nonDiscriminationNotice = Path.Combine(_webHostEnvironment.WebRootPath, @"\Content\pdf\SpanishNondiscrimination.pdf");
  var newPDF = PdfDocument.FromFile(nonDiscriminationNotice);
  PDF.AppendPdf(newPDF);

  return File(PDF.BinaryData, "application/pdf;");

Any suggestions would be helpful

2

There are 2 best solutions below

0
On BEST ANSWER

Dang, thanks for the reminder. So fix was absolutely infuriating. For some reason, there an entry in the host file on server that was affecting how the CSS was being render. That's why it worked locally fine, but then when it was published it was erroring. Super frustrating and took way to long to figure out.

0
On

The RenderingOptions contain a CustomCssUrl property, did you try to use that?

https://ironpdf.com/object-reference/api/IronPdf.ChromePdfRenderOptions.html#IronPdf_ChromePdfRenderOptions_CustomCssUrl