2

我有以下创建 HTML 页面 PDF 的函数。

[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");
}

此功能允许我获取 HTML 页面的 PDF。

但是,它似乎无法添加外部 CSS。

我已将它添加到 HTML 文件的头部。

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

当我直接访问页面“/PDFPreview”时,我得到了正确的 CSS。

关于如何强制 SelectPDF 使用正确的 CSS 的任何建议?

4

1 回答 1

0

尝试在转换之前添加延迟以允许下载字体文件:

// specify the number of seconds the conversion is delayed
converter.Options.MinPageLoadTime = 2;
于 2019-05-13T05:15:30.680 回答