6

I am generating PDF documents successfully in an Azure Web Application written using IronPDF as our PDF rendering engine.  The use case is generate PDFs of financial statements and invoices in C# as part of a wider closed business application.

Everything is working well in local development and HTML To PDF document generator functionality is exactly as we needed. Taking the PDF generator into staging we are noticing slow rendering times.

 These sometimes cascade into a backlog of PDFs in a request queue which can lead to timeouts from web requests when the wait goes over 30 seconds. Our code is not unusual and carefully follows steps in the Ironpdf C# PDF creation tutorial: https://ironpdf.com/tutorials/html-to-pdf/

PM> Install-Package IronPdf

var Renderer = new IronPdf.HtmlToPdf();
Renderer.RenderHtmlAsPdf(templateHTML);

templateHTML is unique for each render, and contains links to CSS and images.

How can our developers enhance PDF rendering performance and enable multithreading for the IronPdf.HtmlToPdf PDF generator class?

Are there known limitations for using IronPdf to generate a PDF file in C# on Azure in .NET?

To be clear, I am not soliciting advice on other 3rd party C# PDF libraries, simply how to best implement and optimize our chosen solution.

To clarify the question given clear context:

  • Which Azure web application or function plan will give optimal HTML to PDF generation performance using IronPDF in .NET Core
  • How can I render multiple PDF documents simultaneously using IronPDF on Azure
Stephanie
  • 600
  • 13
  • 24

2 Answers2

12

I work for IronSoftware as a QA engineer and find myself uniquely able to provide advice on this question about the IronPDF Nuget Packages for .NET.

Which Azure Plan: PDF File Rendering on Azure

For bulk Generating Pdf files in bulk, IronPdf in .NET applications on Azure I would highly recommend using the Basic B1 tier or higher, or the Premium Plan. The Free and Shared tiers, and the Consumption Plan, are not suitable for PDF rendering. The reason for this is that it uses a complete web browser to create PDF files from HTML code.

We need to provision enough resources to treat each render as your browser would treat any web page - it can take a second or two. The IronPDF library works in Azure WebApps, Functions and WebJobs. It works in both Linux and Windows variants, although I would recommend Windows variant Functions and WebApps as significantly better tested and easier to install.

This is simply because Linux support for .NET is more recent. Windows has a longer history and is easier to configure permissions for a PDF generator use case!

Caveat: Rendering SVG fonts into a PDF Document

One known limitation for generating PDF files using C# we have found is that Azure hosting platform does not support servers loading SVG fonts, such as Google Fonts, in their shared web-app tiers. This is because these shared hosting platforms are not allowed to access windows GDI+ graphics objects for security reasons. These restrictions are not present in Linux, or Windows in Docker containers however. Docker containers can be a great way to go.

Reference: https://ironpdf.com/docs/questions/azure/

Multithreaded PDF Generation

At the date of writing IronPdf is thread safe, but not truly multithreaded.

Only one PDF file can be rendered at a time per .NET process. It has been a major drawback on PDF document creation performance until 2021.For example this will run one-by-one even though its called in parallel to generate pdf documents simultaneously.

// using System.Linq;
// using System.Threading.Tasks;

var Renderer = new IronPdf.HtmlToPdf();
// Where Htmls is an iEnumerable of HTML strings 

Parallel.ForEach(Htmls, html =>  
  {  
   Renderer.RenderHtmlAsPdf(html); 
   // works but not faster than formal foreach 
  }  

A solution is found by updating to IronPDF 2021.9 and above:

// PM> Install-Package IronPdf
// using System.Linq;
// using System.Threading.Tasks;IronPdf.Installation.DefaultRenderingEngine = IronPdf.Rendering.PdfRenderingEngine.Chrome;var Renderer = new IronPdf.HtmlToPdf();Parallel.ForEach(Htmls, html =>  
  {  
   Renderer.RenderHtmlAsPdf(html); 
   // works in multiple threads !!
  }  

In short: remove the old IronPDF Nuget package and replace it with IronPDF 2021.9 or above

IronPd 2021.9 is a ground up rebuild of our .NET PDF rendering technology:

Providing effortless multithreading and Async, using as many CPU cores as you wish. For SAAS and high-load applications this may be an order of magnitude faster; outperforming direct browser usage and web-drivers. You can also use the new 2021 API:

// using IronPdf;
// PM> Install-Package IronPdf
ChromePdfRenderer Renderer = new ChromePdfRenderer();
Renderer.RenderingOptions.FitToPaper = true;
Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Screen;
Renderer.RenderingOptions.PrintHtmlBackgrounds = true;
Renderer.RenderingOptions.CreatePdfFormsFromHtml = true;
var doc = await Renderer.RenderHtmlAsPdfAsync ("<h1>Hello world!</h1>");

Azure is a first class citizen of the new IronPDF renderer which is based on the latest "Blink!" HTML string to pdf file rendering! Choose from Chrome Identical PDF creation or Enhanced rendering settings - which we find more accurate and easy to code for than Chrome.

We also recommend this route because it adds Section 508 accessibility compliance in C# PDF Generator : Producing accessible PDFs using the PDF(UA) tagged PDF standard.

The IronPDF team have maintained and improved full support for: .Net 5, Core and Framework 4.0+ support. We also tested it with 1156 green unit & integration tests in the current release (2021.9)

darren
  • 475
  • 4
  • 15
  • 1
    Thank you for an incredibly detailed and fast response. – Stephanie Jul 19 '21 at 06:11
  • I agree that LarryX has a great point about async, though im not an expert on Azure distributed architectures. IronPdf does have Async variants of render methods such as documented here https://ironpdf.com/examples/async/. I found that using using Parallel.ForEach was the highest performing strategy for batch rendering Html To PDF for my use case - and it worked better than async – Stephanie Jul 20 '21 at 02:49
  • 1
    I no longer need to use EAP. IronPDF main branch now supports all of this: https://www.nuget.org/packages/IronPdf/. Thanks @darren – Stephanie Oct 01 '21 at 08:58
  • 1
    Its a pity a regression in this refactor means IronPDF does not run from a package file in Azure anymore which is against the best practice advise from Microsoft. – Adam Jan 04 '22 at 07:42
  • Chrome isolates each browser in its own process for security and performance reasons - as a result, the Chrome renderer used by IronPdf needs sufficient execution permissions within its own deployment directory. "Run from package file" mounts a zip archive as a read-only environment, which creates a few issues for the Chrome renderer. – darren Jan 04 '22 at 16:08
  • Iron Software recognize that some customers would like to use this option to run from a package file, and we are working on a solution that either a) allows the Chrome renderer to operate properly while inside a mounted archive or b) deploys the executable portion of the Chrome renderer to a location outside of the archive. We want to make sure our solution works reliably, which is why we currently recommend not running from the package file when deploying an Azure Function. – darren Jan 04 '22 at 16:09
1

Usually you want to make it async by writing the request into a storage queue, and return immediately. Using Azure function(which is highly scalable) to process the requests from the queue.

Depending on the type of app, you can use a signalR channel to notify the UI that the pdf generation is completed

LarryX
  • 591
  • 2
  • 7