Generating images from PDFs

As we have quite a staff of programmers, we often run into various technical challenges that bring us joy when we overcome them. Today I am going to write about one such recent challenge and how we solved it.

We were developing a complex solution for one of our clients that in one of the use-cases had to display pages of a PDF as images (jpegs). It was alright, as the core solution was PHP and the system was to be hosted on Ubuntu server.

All we had to do is install ImageMagick to the server (also note, that in order to read PDFs Linux also needs Ghostscript). Conversion did work from the command line. But, when calling it from PHP using:

it was too slow and sometimes dropped errors. Minimising density and removing resize didn’t help much. This happened due to the transformations we had to do with the files to display them online. Increasing maximum script execution time in php.ini did give us the result, but a 9-page PDF converting to images in 3 minutes – that was too much.

We were looking for workarounds and finally decided to go the following way:

This gave us a smaller script execution time and a proper result.