我正在使用 NReco 在 Azure 上生成 PDF,该 NReco 使用 WkHtmlToPdf。在我的本地服务器上,一切都生成得很好。然而,在 Azure 上,它会用黑色方 block 呈现所有字体。
我已经尝试了网上能找到的所有方法。
这是我的 HTML:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
@@font-face {
font-family: "FreeSerif";
src: url(@(HttpContext.Current.Server.MapPath("~/Content/FreeSerif.ttf"))) format("truetype");
}
* {
font-family:"FreeSerif", Helvetica, Arial, sans-serif;color:black;
}
</style>
</head>
我也尝试过使用 Url.Content("~/Content/FreeSerif.ttf")
还有我的 C#:
string htmlText = RenderPartialViewToString("~/Views/Templates/PDF/ListPDFView.cshtml", pdfList);
HtmlToPdfConverter nPdf = new HtmlToPdfConverter();
nPdf.Size = PageSize.Letter;
nPdf.Orientation = PageOrientation.Landscape;
nPdf.CustomWkHtmlArgs = "--encoding UTF-8";
pdfBuf = nPdf.GeneratePdf(htmlText);
Response.ContentType = "application/pdf";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "Inline; filename=file.pdf");
Response.BinaryWrite(pdfBuf);
Response.Flush();
Response.End();
我的 web.config 包含:
<system.webServer>
<staticContent>
<remove fileExtension=".ttf" />
<remove fileExtension=".svg" />
<remove fileExtension=".eot" />
<remove fileExtension=".woff" />
<mimeMap fileExtension=".ttf" mimeType="font/truetype" />
<mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
<mimeMap fileExtension=".eot" mimeType="application/vnd.ms-fontobject" />
<mimeMap fileExtension=".woff" mimeType="application/x-woff" />
</staticContent>
</system.webServer>
每个解决方案都会产生黑色方 block 。我在这里束手无策。非常感谢您的帮助。
最佳答案
不幸的是,如果 Azure 表示“Azure 网站”,则 WkHtmlToPdf 不支持它,并且 NReco.PdfGenerator page 上的常见问题解答部分提到了这一点。 。 在您的情况下,似乎生成的 PDF 没有错误,但出现黑色方 block ,因为 WkHtmlToPdf 使用 Windows GDI API,该 API 在 Azure 网站上不起作用。
尽管如此,PdfGenerator 对于 Azure WebRole 或 Azure VM 应该可以正常工作。 在这种情况下,Amazon EC2 T2 微型实例也是 Azure 网站的良好替代方案。
--- 更新 ---
目前,Azure 应用程序(以前的网站)具有基于 VM 的订阅(除“免费”和“共享”之外的所有计划),托管环境限制较少,允许 wkhtmltopdf 执行,在这种情况下可以使用 NReco PdfGenerator 包装器。请注意,仍然存在一些限制:例如,不会呈现自定义字体(只能使用系统安装的字体)。
关于c# - NReco PDF 在 Azure 上有黑色方 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25792948/