3
votes

I am using the great tool wkhtmltopdf to render HTML into nice PDF documents (executed from PHP). I now need to know if the HTML page I will render will fit onto "one page" in the PDF. I found out wkhtmltopdf will render ~ 1100px height on one page with my current settings.

I came up with the idea to use wkhtml2image first. I can then get the image dimensions which works fine.

Unfortunately the rendering to an image takes a long time and since I have to do it in a loop I'm looking for a faster way to do this.

Do you know any tool that will render HTML with webkit and give me back the dimensions of the resulting document? It might also be possible to use JavaScript as a helper (write dimensions into a div that can be extracted after) but I can't find a tool which gets the job done.

Any ideas or alternative approaches?

1
An idea: You could invalidate all the images in the document and possibly render it faster. Did you try just substituting all occurrences of jpeg/gif/png with "invalid"? Just an idea.Janus Troelsen
@Janus - that could make the layout to break apart, or what if a whole page is one image, i.e. a graph... not a good idea...Michal
you have to patch wkhtmltopdf and re-patch QT to be able to that, otherwise it won't work. I'm assuming that's a lot of work. There is at least one web service based on wkhtmltopdf that does what you want. It has an API at htm2pdf.co.uk/html-to-pdf-api which could help you.user1914292

1 Answers

1
votes

As far as I know there is no reliable way how to count the pages in advance of generating the PDF file itself. This is due to the way how WebKit renders lines and how the pages are being broken or something (more info here).

However, you could use another tool to count the pages in a already generated PDF file.

You could also look @ my other extensive post regarding WKHTMLTOPDF here HTML2PDF in PHP - convert utilities & scripts - examples & demos. Maybe you find useful info there and my code there also features page counting on a already generated PDF file (in the process of encrypting it - a FPDF/FPDI implementation).