2

I have the following function that creates a PDF of a HTML page.

[Authorize]
public FileStreamResult PDFCV(int Id)
{
   var user = _userManager.GetUserAsync(User);
   HtmlToPdf converter = new HtmlToPdf();
   var BaseUrl = HttpContext.Request.Host;
   var Path = Url.Action("PreviewCv", "Cv", new { Id = Id });
   try
   {
       converter.Options.HttpCookies.Add(".AspNetCore.Identity.Application", HttpContext.Request.Cookies[".AspNetCore.Identity.Application"]);
   }
   catch (Exception e)
   {
      Console.WriteLine(e);
   }
   string url = "";
   try
   {
      url = BaseUrl + Path;
   }
   catch (Exception e)
   {
      Console.WriteLine(e);
   }
   try
   {
      PdfDocument doc = converter.ConvertUrl(url);
      var PdfArray = doc.Save();
      doc.Close();
      return new FileStreamResult(new MemoryStream(PdfArray), "application/pdf");
   }
   catch (Exception e)
   {
     Console.WriteLine(e);
   }

   return new FileStreamResult(new MemoryStream(), "application/pdf");
}

This function allows me to get the PDF of the HTML page.

However it seems like it is not able to add the external CSS.

I have added it to the head of the HTML file.

<head>
   <link href="https://fonts.googleapis.com/css?family=Montserrat" 
</head>

When I access the page directly "/PDFPreview", I get the correct CSS.

Any suggestions on how I could force SelectPDF to use the correct CSS?

Atle Kristiansen
  • 707
  • 7
  • 28

2 Answers2

0

Try adding a delay before the conversion to allow the font files to download:

// specify the number of seconds the conversion is delayed
converter.Options.MinPageLoadTime = 2;
Tom
  • 285
  • 2
  • 3
  • 1
    I did, nothing changes. I even tried a unrealistic number (like 60) the PDF is still incorrect. – Atle Kristiansen May 13 '19 at 10:11
  • See if this is the case for you: untrusted fonts blocked - https://learn.microsoft.com/en-us/windows/security/threat-protection/block-untrusted-fonts-in-enterprise – Tom May 15 '19 at 12:07
  • Why would they work when I access the page directly if they are blocked? Or do you mean that they get blocked server side, but are allowed on client side? – Atle Kristiansen May 15 '19 at 18:22
  • Struggling with same/similar issue - posted issue on the selectpdf github repo hoping for an answer there. https://github.com/selectpdf/selectpdf-free-html-to-pdf-converter/issues/11 – Christopher Oct 07 '20 at 12:53
  • Update: Actually my issue was just a path issue on my end ... web fonts seem to be working great in HTML to Image and HTML to PDF modes. – Christopher Oct 07 '20 at 13:14
0

I was using the ConvertUrl() method in Select.Pdf.NetCore and was getting the wrong font. I found it started working when I switched to the Select.Pdf.NetCore.Blink package. I had to add the following line:

converter.Options.RenderingEngine = RenderingEngine.Blink;