Sayan Pal
Sayan Pal

Reputation: 4946

How to show text in PDF generated by Azure function

I am trying to generate a PDF via Azure function using DinkToPdf. This what I have done so far.

[FunctionName("GeneratePdf")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    ILogger log,
    ExecutionContext executionContext)
{
    string name = await GetName(req);
    return CreatePdf(name, executionContext);
}

private static ActionResult CreatePdf(string name, ExecutionContext executionContext)
{
    var globalSettings = new GlobalSettings
    {
        ColorMode = ColorMode.Color,
        Orientation = Orientation.Portrait,
        PaperSize = PaperKind.A4,
        Margins = new MarginSettings { Top = 10 },
    };
    var objectSettings = new ObjectSettings
    {
        PagesCount = true,
        WebSettings = { DefaultEncoding = "utf-8" },
        HtmlContent = $@"
               <!DOCTYPE html>
               <html>
               <head>
                   <meta charset="utf-8" />
                   <title></title>
                   <meta name="viewport" content="width=device-width, initial-scale=1">
               </head>
               <body>
                  Hello, ${name}
               </body>
               </html>",
     };

     var pdf = new HtmlToPdfDocument()
     {
        GlobalSettings = globalSettings,
        Objects = { objectSettings }
     };

     byte[] pdfBytes = IocContainer.Resolve<IConverter>().Convert(pdf);
     return new FileContentResult(pdfBytes, "application/pdf");
}

This is working pretty good, when I am testing the function on local. However, it is not working as expected when deployed to Azure.

The primary problem is that in the places of the texts in the pdf, boxes are appearing (see below for example). enter image description here

Moreover the response is also excruciatingly slow. Is there a way to improve/correct this?

Additional Info:

  1. I am also using unity IOC to resolve IConverter. The type registration looks something like below:

    var container = new UnityContainer();
    container.RegisterType<IConverter>(
        new ContainerControlledLifetimeManager(),
        new InjectionFactory(c => new SynchronizedConverter(new PdfTools()))
    );
    
  2. I have tried couple of other NuGet packages such as PdfSharp, MigraDoc, Select.HtmlToPdf.NetCore, etc. But alll of those have dependency on System.Drawing.Common, which is not usable in Azure function.

Upvotes: 2

Views: 1582

Answers (1)

lafe
lafe

Reputation: 113

The issue appears to be related to the restrictions of the Azure Function in "Consumption" mode. If you use "App Mode", it should work. See the discussion below this Gist for some users who had success converting their Azure Function to "App Mode".

Upvotes: 4

Related Questions