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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With