You are not logged in.

#1 2015-09-09 08:47:11

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 861

C++ library for fast rendering of a PDF to a PNG image

I need to convert PDF files with many pages containing huge vector images with hundreds of thousands of vector elements each. I tried with the Magick++ API of ImageMagick and it works but it is incredibly slow. Is there any free C++ library with better performance than ImageMagick in converting PDFs to PNGs? Thanks.

Offline

#2 2015-09-09 10:52:18

der_joachim
Member
From: the Netherlands
Registered: 2008-12-17
Posts: 143
Website

Re: C++ library for fast rendering of a PDF to a PNG image

Maybe you can use the Ghostscript API? AFAIK, it is C++ compatible.


Geek, runner, motorcyclist and professional know-it-all

Offline

#3 2015-09-09 10:53:18

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 861

Re: C++ library for fast rendering of a PDF to a PNG image

der_joachim wrote:

Maybe you can use the Ghostscript API? AFAIK, it is C++ compatible.

Thanks, I'll give it a try. I hope it is much faster than ImageMagick...

Offline

#4 2015-09-09 11:19:07

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: C++ library for fast rendering of a PDF to a PNG image

You can also try poppler. The pdftocairo binary supports png output, so you can use it to test the speed.

Last edited by progandy (2015-09-09 11:20:18)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Online

#5 2015-09-09 11:27:49

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: C++ library for fast rendering of a PDF to a PNG image

Progandy beat me to it, but I second the recommendation of both poppler and pdftocairo - you may not need to write any code at all as this is exactly what pdftocairo does.  I've used poppler to a good degree, and only occasionally used gs and imagemagick - while I've never done proper side-by-side comparisons poppler has always felt like it rendered pdfs quite a bit faster to me.

That said, if there are hundreds of thousands of vector elements there will be a floor effect of regardless how efficient the rendering library is, the process will be CPU limited and may take a little time.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2015-09-09 12:50:15

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 861

Re: C++ library for fast rendering of a PDF to a PNG image

Thanks for the suggestions. I think poppler is not efficient enough since AFAIK it is the backend of okular, which is really slow in displaying the PDF. Anyway I will try it and also pdftocairo.

Offline

#7 2015-09-09 13:13:00

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: C++ library for fast rendering of a PDF to a PNG image

I forgot that mupdf comes with mudraw that can do the conversion, too.

I created a pdf from a complex svg image and tested it with imagemagick, poppler, ghostscript and mupdf.
Edit: added inkscape, graphicsmagick
https://commons.wikimedia.org/wiki/File:Arctic_big.svg
Here are my results:

$ time (
inkscape test.pdf -z --export-dpi=300 --export-area-drawing --export-png=test.ink.png)
DPI: 300
Background RRGGBBAA: ffffff00
Area 0:151,562:945:1334,99 exported to 3150 x 3945 pixels (300 dpi)
Bitmap saved as: test.ink.png
====
25,56s user 1,52s system 98% cpu 27,358 total

$ time (
convert -density 300 test.pdf test.png)
====
21,82s user 0,82s system 99% cpu 22,844 total

$ time (
pdftocairo -png -r 300 test.pdf test.poppler)           
====
13,68s user 0,13s system 99% cpu 13,941 total

$ time (     
pageNum=`gs -q -dNODISPLAY -c "(./test.pdf) (r) file runpdfbegin pdfpagecount = quit"`
gs -dNumRenderingThreads=4 -dNOPAUSE -sDEVICE=pngalpha -dFirstPage=1 -dLastPage=$pageNum -sOutputFile=./test.gs.%d.png -r300 -q test.pdf -c quit)
====
10,57s user 0,07s system 110% cpu 9,613 total

$ time (
gm convert -density 300 test.pdf test.gm.png)
====
8,81s user 0,25s system 98% cpu 9,160 total


$ time (       
mudraw -r 300 -o test.mu.%d.png test.pdf)
====
 5,16s user 0,25s system 99% cpu 5,442 total

Last edited by progandy (2015-09-09 13:33:00)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Online

#8 2015-09-09 13:23:19

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: C++ library for fast rendering of a PDF to a PNG image

Thanks for a real test Progandy - I've always liked mupdf, and have thought about converting some of my tools to it.  But despite how well the program and library always seem to work, I've never found suitable API documentation.  But if mudraw will do the trick that may be best.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#9 2015-09-10 14:46:20

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 861

Re: C++ library for fast rendering of a PDF to a PNG image

I did some trials with the softwares you suggested. With the typical PDFs I must handle I get 20m54s with pdftocairo and 9m3s with mudraw which seems to be the faster one. These times are still not acceptable for me, I need to rework all my code to avoid this PDF->PNG conversion...
Thanks to everybody for the help.

Offline

#10 2015-09-10 15:02:18

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: C++ library for fast rendering of a PDF to a PNG image

There are some other thing you can try like using GIMP or LibreOffice. Maybe Inkscape scales better when it comes to more complex pages, ... The startup costs of these applications might be trivial if the conversion consumes a lot of time.


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Online

#11 2015-09-10 15:07:51

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 861

Re: C++ library for fast rendering of a PDF to a PNG image

But I need to do this programmatically, so I'd need a library and an API in C or C++. Do GIMP or LibreOffice or Inkscape provide such things?

Offline

#12 2015-09-10 15:15:16

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: C++ library for fast rendering of a PDF to a PNG image

snack wrote:

But I need to do this programmatically, so I'd need a library and an API in C or C++. Do GIMP or LibreOffice or Inkscape provide such things?

They provide commandline options, so you can script the conversion with fork/exec.


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Online

#13 2015-09-10 15:16:57

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 861

Re: C++ library for fast rendering of a PDF to a PNG image

I'll give it a try, but I doubt that these GUI programs are available on the compute nodes of the pc farm of my institute. Anyway, thanks again progandy!

Offline

Board footer

Powered by FluxBB