Reputation: 770
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?
Upvotes: 3
Views: 5071
Reputation: 3368
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).
Upvotes: 1