【问题标题】:NReco PDF has black squares on AzureNReco PDF 在 Azure 上有黑色方块
【发布时间】: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


【解决方案1】:

如果 Azure 表示“Azure 网站”,很遗憾 WkHtmlToPdf 不支持它,这在 NReco.PdfGenerator page 上的常见问题解答部分中提到。 在您的情况下,似乎 PDF 生成时没有错误,但出现黑色方块,因为 WkHtmlToPdf 使用了在 Azure 网站上不起作用的 Windows GDI API。

尽管如此,PdfGenerator 应该适用于 Azure WebRole 或 Azure VM。 在这种情况下,Amazon EC2 T2 微实例也是 Azure 网站的不错替代方案。

--- 更新 ---

目前,Azure 应用程序(以前的网站)具有基于 VM 的订阅(除“免费”和“共享”之外的所有计划),托管环境限制较少,允许执行 wkhtmltopdf,并且可以在这种情况下使用 NReco PdfGenerator 包装器。请注意,仍然存在一些限制:例如,不呈现自定义字体(只能使用系统安装的字体)。

【讨论】:

  • 然后可以做些什么来生成没有图片的PDF?还有另一个可以证明的库或解决问题的一些技术
【解决方案2】:

我有一个类似的问题要解决这个问题,我必须创建一个 Azure 云服务来生成 pdf 格式的报告。创建云服务并正确生成 pdf,在我的案例中声明的每种样式我使用旋转并且正在努力完善留下一些链接,以便您可以看到如何为云服务创建项目 azure

Cloud Services Example

https://www.visualstudio.com/en-us/docs/release/examples/azure/net-to-azure-cloud-services

https://github.com/Microsoft/nodejstools/wiki/Azure-Cloud-Service-Projects

【讨论】:

    猜你喜欢
    • 2017-04-09
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 2014-05-20
    • 1970-01-01
    • 2016-02-06
    相关资源
    最近更新 更多