DinkToPdf icon indicating copy to clipboard operation
DinkToPdf copied to clipboard

Azure Functions - black squares

Open rybczak opened this issue 6 years ago • 10 comments

After publishing v2 function into Azure, my pdf file contains black squares instead of correct font. I'm not setting font explicitly so I assume that default one should be taken on Azure instance. On local machine everything works fine, as always.

Code was deployed to azure before and worked fine for a long time. Have you got any ideas what can be wrong? I'm running out of ideas.

rybczak avatar Nov 12 '18 23:11 rybczak

After publishing v2 function into Azure, my pdf file contains black squares instead of correct font. I'm not setting font explicitly so I assume that default one should be taken on Azure instance. On local machine everything works fine, as always.

Code was deployed to azure before and worked fine for a long time. Have you got any ideas what can be wrong? I'm running out of ideas.

Hello. Have you solved it? I have the same problem

DiyazY avatar Nov 25 '18 21:11 DiyazY

Sorry for very late response. Problem was in Azure Function settings - after deployment service plan changed to 'Consumption'. At least Basic is required.

rybczak avatar Feb 05 '19 09:02 rybczak

Hello @rybczak !

I'm also trying to implement a HTML to PDF converter with Azure Functions v2 NET Core and DinkPDF, I get no runtime exceptions, but the execution of the function gets terminated once I hit the convert method of the converter.

[FunctionName("GeneratePDF")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            if (name != null)
            {
                CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
                context.LoadUnmanagedLibrary($"{Directory.GetCurrentDirectory()}/Dependencies/libwkhtmltox.dll");

                var converter = new BasicConverter(new PdfTools());

                var doc = new HtmlToPdfDocument()
                {
                    GlobalSettings = {
                        ColorMode = ColorMode.Color,
                        Orientation = Orientation.Landscape,
                        PaperSize = PaperKind.A4Plus,
                    },
                    Objects = {
                            new ObjectSettings() {
                                PagesCount = true,
                                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>",

                                WebSettings = {
                                    DefaultEncoding = "utf-8"
                                },
                                HeaderSettings = {
                                    FontSize = 9,
                                    Right = "Page [page] of [toPage]",
                                    Line = true,
                                    Spacing = 2.812
                                }
                            }
                        }
                };


                var bytes = converter.Convert(doc);

                return new FileContentResult(bytes, "application/pdf");
            }


            return new BadRequestResult();
        }

No exceptions popping out, no information.

Could you please lend a hand to me? Thank you!

alextof avatar Feb 09 '19 16:02 alextof

Hello @rybczak !

I'm also trying to implement a HTML to PDF converter with Azure Functions v2 NET Core and DinkPDF, I get no runtime exceptions, but the execution of the function gets terminated once I hit the convert method of the converter.

[FunctionName("GeneratePDF")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            if (name != null)
            {
                CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
                context.LoadUnmanagedLibrary($"{Directory.GetCurrentDirectory()}/Dependencies/libwkhtmltox.dll");

                var converter = new BasicConverter(new PdfTools());

                var doc = new HtmlToPdfDocument()
                {
                    GlobalSettings = {
                        ColorMode = ColorMode.Color,
                        Orientation = Orientation.Landscape,
                        PaperSize = PaperKind.A4Plus,
                    },
                    Objects = {
                            new ObjectSettings() {
                                PagesCount = true,
                                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>",

                                WebSettings = {
                                    DefaultEncoding = "utf-8"
                                },
                                HeaderSettings = {
                                    FontSize = 9,
                                    Right = "Page [page] of [toPage]",
                                    Line = true,
                                    Spacing = 2.812
                                }
                            }
                        }
                };


                var bytes = converter.Convert(doc);

                return new FileContentResult(bytes, "application/pdf");
            }


            return new BadRequestResult();
        }

No exceptions popping out, no information.

Could you please lend a hand to me? Thank you!

Regarding this issue, I could manage to make it work changing var converter = new BasicConverter(new PdfTools()); to var converter = new SynchronizedConverter(new PdfTools());

alextof avatar Feb 09 '19 16:02 alextof

Hello @rdvojmoc ,

I'm also facing the issue that the converted PDF file has black squares. azure plan is : consumption plan. but it works in local.

nrajasekar7 avatar Aug 30 '19 02:08 nrajasekar7

Hello @rdvojmoc

After publishing v2 function into Azure, my pdf file contains black squares instead of correct font. I'm not setting font explicitly so I assume that default one should be taken on Azure instance. On local machine everything works fine, as always.

Code was deployed to azure before and worked fine for a long time. Have you got any ideas what can be wrong? I'm running out of ideas.

Any work around in consumption plan?

nrajasekar7 avatar Aug 30 '19 02:08 nrajasekar7

@nrajasekar7 you ever find a fix? I prefer staying in consumption plan as there is better performance. Hoping you figured out a solution. I'm getting black boxes / black squares...

markti avatar May 17 '20 14:05 markti

@markti I am sorry. I didn't get any work around

nrajasekar7 avatar Jun 10 '20 13:06 nrajasekar7

The sandbox limitation is not in place for Linux. I deployed to a Linux comsumer plan / serverless and it worked fine.

JoostVanVelthoven avatar Aug 17 '20 17:08 JoostVanVelthoven

Hello all, same as you guys, I got black square for tiny html but with bigger Html the solution goes TimeOut ! I am working on a PDF solution from 1 month now, did somebody has a solution ?

I use Azure Function Y plan with DinkToPdf. In local its work perfectly but then in Azure server its always breaking the service !

Syn0 avatar May 05 '22 15:05 Syn0