PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Bulk rendering PDFs (in memory!)
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3018
Page 1 of 1

Author:  heyseuss [ Tue Dec 23, 2014 2:53 am ]
Post subject:  Bulk rendering PDFs (in memory!)

New user to MigraDoc (and PDF generation in general), like the library very much so far. I am going to use it to generate 5000-20000 single-page PDFs at a time, put them into a zip file, and allow users to export many customers. Fun! I am currently trying to create 10000 PDFs, as a test case and learning experience, using the Invoice sample application (the GDI+ C# one). I have basically just said "use the normal Invoice class and loop a lot of times":

Code:
using (var stream = new MemoryStream())
{
    using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
    {
        for (int i = 0; i < 10000; i++)
        {
            Trace.TraceInformation("Starting {0}", i);
 
            var entry = archive.CreateEntry(string.Format("{0}.pdf", i));
 
            var invoice = new InvoiceForm("../../invoice.xml");
            var document = invoice.CreateDocument();
            var renderer = new PdfDocumentRenderer(true) {Document = document};
            renderer.RenderDocument();
           
            using(var rendererStream = new MemoryStream())
            using (var zipEntryStream = entry.Open())
            {
                renderer.Save(rendererStream, false);
               
                rendererStream.CopyTo(zipEntryStream);
            }
        }
    }
    var bytes = stream.ToArray();
}


I understand that this is not smart and probably recommended only for small collections of files. I run OOM @ around 1000 PDFs created in a ASP.NET application, and around 5000 in a console application (it tries so hard though: http://i.imgur.com/Q37l3Z9.png).

Can I get any more mileage out of this approach, or should I just stop now and create all the files locally one by one and zip normally? Is there any way to pre-compile/render/template the document so that I could create a simple instance of it, replacing only 1-2 fields of custom data (like the address fields in the invoice sample but not the overall layout)? Not sure if that is the right terminology.

Author:  Thomas Hoevel [ Mon Jan 12, 2015 9:39 am ]
Post subject:  Re: Bulk rendering PDFs (in memory!)

Hi!

The image shows 406 MB total memory and 328 MB large object heap. Is that the point when the application crashes?

32 bit applications should be able to handle up to 2000 MB memory and 64 bit applications should handle even more.

Since .NET 4.5.1 you can invoke a garbage collection for the large object heap. This might take you a bit further:
http://msdn.microsoft.com/en-us/magazine/cc534993.aspx

But IMHO it's probably better to save the PDF files to disk and then combine them.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/