【发布时间】:2014-09-11 16:48:19
【问题描述】:
我正在使用 NReco 在 Azure 上生成 PDF,该 NReco 使用 WkHtmlToPdf。在我的本地服务器上,一切都很好。然而,在 Azure 上,它用黑色方块渲染所有字体。
我已经尝试了所有可以在网上找到的东西。
这是我的 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>
每个解决方案都会产生黑色方块。我在这里束手无策。非常感谢您的帮助。
【问题讨论】:
-
作为一种调试技术,为什么不尝试只使用标准的内置 Windows 字体(或从 CSS 中完全删除字体定义)。只是看看你是否可以让它生成任何可读的东西。
-
我尝试过 Helvetica、Arial、Verdana、'Arial MS Unicode'、Tahoma、Georgia、sans-serif。这些都不起作用。
-
没有怎么样?只需省略 font-face 声明,看看会发生什么。
-
原来没有指定字体。然后我将它部署到 Azure 时出现了黑色方块。
标签: c# pdf azure wkhtmltopdf