Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

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.

like image 956
brenjt Avatar asked Sep 11 '14 16:09

brenjt


1 Answers

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

like image 100
Vitaliy Fedorchenko Avatar answered Oct 11 '22 13:10

Vitaliy Fedorchenko