简体   繁体   中英

NReco PDF has black squares on Azure

I am generating PDFs on Azure using NReco which uses WkHtmlToPdf. On my local server everything generates just fine. On Azure however it renders all fonts with black squares.

在此输入图像描述

I have tried everything I can find online.

Here is my 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>

I have also tried using Url.Content("~/Content/FreeSerif.ttf")

And my 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();

My web.config contains:

<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>

Every solution results in black squares. I am at my wits end here. Your help is very appreciated.

If Azure means "Azure Websites" unfortunately it is not supported by WkHtmlToPdf and this is mentioned FAQ section on NReco.PdfGenerator page . In your case it seems PDF is generated without errors but black squares appears because WkHtmlToPdf uses Windows GDI API which doesn't work at Azure WebSites.

Nevertheless PdfGenerator should work fine for Azure WebRole or Azure VM. Amazon EC2 T2 micro instance is also good alternative for Azure WebSites in this case.

--- UPDATE ---

For now Azure Apps (former Websites) has VM-based subscriptions (all plans except "Free" and "Shared") with less restrictive hosting environment that allows wkhtmltopdf execution and NReco PdfGenerator wrapper can be used in this case. Note that some limitations still exist: for example, custom fonts are not rendered (only system-installed fonts could be used).

I had a similar problem to solve this had to create a cloud service azure to generate reports in pdf. to create the cloud service and the pdf is generated correctly with every style declared in my case I use rotary and is working to perfection leave some links so you can see how to create a project for cloud services 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM