Slow conversion PDF -> PNG for documents with big page-size

Hello,

we’re using Aspose.PDF (C#) to render PDF-Pages to PNG for a preview function in our application.

User report state, that for some documents this takes excessive time (~10-20s). We could reproduce this issue on our dev machines with sample documents taking ~8s to render a single page.
Pdf Viewers like Adobe Reader or Edge open these documents just fine without any real delay.

It seems like the problematic documents have in common, that they contain a single image (photograph) beeing converted to a pdf with a very big page size. The size of the image in pixels does not seem to be that relevant for this issue.

Due to privacy reasons we can’t provide a real document, but we managed to create one using an online PDF converter that behaves close to what we see with user documents (although not as slow with around 3s conversion for a single page on our machines).

The RenderingOptions of the PngDevice do not seem to affect performance in any way (except of OptimizeDimensions which roughly doubles the time), also the resolution is not relevant.

A simple way to reproduce this is as follows:

using System.Diagnostics;
using Aspose.Pdf;

License pdfLicense = new();
//TODO Load License

using MemoryStream pdfStream = new(File.ReadAllBytes("slow_convert_sample.pdf"));

using Document document = new(pdfStream);

for (int i = 0; i < 3; i++)
{
    foreach (Page page in document.Pages)
    {
        Stopwatch sw = Stopwatch.StartNew();
        using MemoryStream ms = page.ConvertToPNGMemoryStream();
        sw.Stop();
        Console.WriteLine(sw.Elapsed);
    }
}

slow_convert_sample.pdf (1,5 MB)

@bernd.buehler
Is it possible to remove sensitive information from such problematic documents to provide more representative example? We would be very grateful for that.
I’ll try to reproduce your issue by description as 3 seconds doesn’t seem too much to be an issue - especially when compared to around 15 seconds as you described in original

@ ilyazhuykov
I don’t see a way to do that. Since the document contains only a single image with sensible data we would need to modify/replace the image which changes the behavior of the document.

But trying this I found some more things that might be interesting:

  1. The PNG-creation seems to be really slow, so for now we changed everything to the BmpDevice to eliminate that as a factor.
  2. Creating the same document provided above with Aspose instead of the online tool (same image, same page dimensions) leads to a significantly bigger document that performs way better when rendering the page. I attached that document.

Looking at a performance trace (images attached) it seems like something in the slow pdf causes the usage of a different (worse) code path at some point. Sadly due to the obfuscation we can’t tell what that is.

Any insight regarding the cause of this difference would be really helpful for us.

We can’t control the document creation on our customers side, but knowing what causes the issue would at least allow us to give some guideline or work around it at our side by detecting “bad” documents.

sample_converted_with_aspose.pdf (4,0 MB)

onlineConverter_Document_trace.png (214,1 KB)

Aspose_Document_trace.png (173,7 KB)

1 Like