c# - NReco PDF 在 Azure 上有黑色方 block

标签 c# pdf azure wkhtmltopdf

我正在使用 NReco 在 Azure 上生成 PDF,该 NReco 使用 WkHtmlToPdf。在我的本地服务器上,一切都生成得很好。然而,在 Azure 上,它会用黑色方 block 呈现所有字体。

enter image description here

我已经尝试了网上能找到的所有方法。

这是我的 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/

相关文章:

c# - 具有泛型的约束语法,也从类派生

c# - 为什么我不能声明一个从 Byte 继承的枚举但我可以从 byte 继承?

java - 应用于 Paragraph 的 itext spacingBefore 属性导致新页面

c# - PDF如何预览快照?

c# - 每次调用或存储在静态变量中时读取环境变量吗?

Azure 网站和 VIP 交换

c# - 在列表 C# 中加入具有相同参数的条目

java - 下载完成后自动打开文件PDF

azure - .net core Azure Functions启动类未被调用

c# - 如何在 C# 中调整 Windows 窗体窗体的大小?