11

I've got an issue with PDF generation currently my system is using dompdf to convert HTML to PDF this all works fine. However if the user inserts an image that is bigger than an A4 page the PDF screws up badly and all the content below the large image gets bunched up or isn't shown at all.

Here are two PDFs to show an example of the issue

This one is fine

https://dl.dropbox.com/u/65878041/OK.pdf

This one you can see with the first image being bigger than the A4 page all the content following gets screwed up when it should be the same as the previous one.

https://dl.dropbox.com/u/65878041/Issue.pdf

What I'd ideally like to do is break the image into parts and span it across as many pages as required. Like what is accomplished in this LaTeX solution. Is there any possibility of doing this with dompdf? or any other PHP library for doing HTML to PDF?

I've looked into it myself and obviously I could chop up an image if its bigger than the A4 page. But problem is with that approach that need to know if there is text on the page before hand or anything like that so its not quite that simple.

Thank you all in advance. Any insight would be very much appreciated.

Community
  • 1
  • 1
Mark Davidson
  • 5,503
  • 5
  • 35
  • 54
  • 2
    It's not a direct solution, but you could consider switching to another HTML-based PDF generator. I've had good luck with `wkhtmltopdf`, which uses webkit. This means you can use CSS and even Javascript to manipulate the page contents while it's being rendered. For example, you could measure the image against the expected viewport, as rendered, then resize as appropriate. I wouldn't vouch for the quality of the resize, of course... – Charles Dec 13 '12 at 07:37
  • What's the DOMPDF version ? I had a similar problem and latest beta did the trick for me. – AKS Dec 17 '12 at 09:15
  • It's 0.6.0 beta 3 which is the latest but I will try pulling the absolute latest version from github. – Mark Davidson Dec 17 '12 at 09:28
  • when an image size more than A4 size then you fix theimage size to display (or) resize the image – Daya Dec 19 '12 at 09:59
  • @MarkDavidson Would love to see some source HTML so we can see where dompdf is failing with this document. – BrianS Jan 11 '13 at 03:10

2 Answers2

5

It would me much more helpful if you posted you html and css... Got two things in mind:

Option #1

Try to wrap your images in the additional DIV and explicitly set the width and overflow rules.

Option #2

Before I was a big fan of dompdf but after discovering wkhtmltopdf never looked back. wkhtmltopdf is a phenomenal tool that gets job done right. To try this solution do the following:

  1. Download static binary from project home page, it's architecture specific so pick the right one... you can download both; at runtime detect architecture and load appropriate binary
  2. Place the file in some directory accessible by your project, e.g.
    /path/to/my/proj/bin/wkhtmltopdf
  3. Run your script and generat html document; save it to some tmp file
    file_put_contents('/tmp/myfile.html', $html);
  4. Run the conversion command
    shell_exec('/path/to/my/proj/bin/wkhtmltopdf /tmp/myfile.html /tmp/myfile.pdf')
  5. Get contents of generated pdf, change response headers and server the file:
    header('Content-Type: application/pdf');

    echo file_get_contents('/tmp/myfile.pdf');

Mark Davidson
  • 5,503
  • 5
  • 35
  • 54
Alex
  • 6,441
  • 2
  • 25
  • 26
1

I don't know how the images are being shown, but you can tell dompdf to split pages after or before a certain element. For example:

<div style='page-break-after:always;'>
    <img src="PATH_TO_IMAGE" />
</div>
ennovativemedia
  • 361
  • 1
  • 7
  • When page will break ? if contains 100 images. i need to break if overflow the div or overflow the A4 Sheet. – 151291 Apr 27 '17 at 06:01