Dicpherrr
Dicpherrr

Reputation: 117

Azure Function - PDF Sharp HTML to PDF - Cannot save a PDF document with no pages

I am using PDF Sharp to take HTML I give it and generate a PDF. The code below works locally but when deployed to an Azure Function and hitting from Postman it gives the error "Cannot save a PDF document with no pages.".

I've seen a few different articles on the PDF Sharp Forums but nothing that solved my issue.

using PdfSharp.Pdf;
using PdfSharp;
using PdfSharp.Drawing;
using TheArtOfDev.HtmlRenderer.PdfSharp;

public byte[] HtmlToPDF(string html)
        {
            
            try
            {
                byte[] pdfBytes;

                using (var ms = new MemoryStream())
                {
                    System.Text.Encoding.RegisterProvider(
                        System.Text.CodePagesEncodingProvider.Instance
                    );
                    
                    // Breaks on this line
                    var pdf = PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
                    pdf.Save(ms);
                    pdfBytes = ms.ToArray();
                }

               return pdfBytes;

            }
            catch (Exception ex)
            {
                throw new Exception(ex);
            }            

        }

Not exactly sure why it's working locally but not deployed on an Azure Function. The HTML I am passing locally is the save as what I'm passing to the Azure Function.

Upvotes: 0

Views: 890

Answers (1)

Dicpherrr
Dicpherrr

Reputation: 117

Figured it out, if anyone has issues in the Future trying to use this with .NET Core. I uninstalled TheArtOfDev version and found another package called HtmlRendererCore.PdfSharp by J-Petty.

Once installed should work very similar to the old one. https://github.com/j-petty/HtmlRendererCore

Upvotes: 1

Related Questions