When using <cfdocument format="PDF" how do you ...
# cfml-general
g
When using <cfdocument format="PDF" how do you make pagebreaks gracefully when outputting data in a pdf?
If it was just single rows of data I guess I could get a count of the rows and force a pagebreak after so many rows. But the data outputted wraps into multiple lines.
m
been a bit since I wrote a PDF report with breaks but I seem to remember that what I ended up with was a div like this:
Copy code
<div style="page-break-before:always">&nbsp;</div>
and occasionally using:
Copy code
<cfdocumentitem type="pagebreak"/>
Again, been a minute, but those seemed to work. Something to keep in mind is to make sure your HTML is valid. If something in your loop messes up your HTML (doesn't close a tag properly, invalid table structure, etc) for whatever reason it can cause issues with breaks rendering correctly.
d
You can also try <cfdocument type=“pagebreak”> which will break on the page. You can then put new headers and such on the remaining pages.
g
Putting a <div style="page-break-before:always"> around each table of data works forcing a pagebreak. But <div style="page-break-inside: avoid;"> does nothing.
I googled and see the page-break-inside property has been replaced by break-inside. But that doesn't seem to work either.
Is it because I have a table tag inside the div?
m
Possibly, I believe that might not be valid.
Some of the validity rules in HTML give me a cough lol.
g
Lemme try structuring t he stuff without tables in the div and see how it works.
1
No, I got rid of the table/tr/tds inside the div and the <div style="break-inside: avoid;"> still does nothing. The page breaks in the middle of the data.
It seems like the cfdocumentitem is the only thing that works. But I would have to count characters and predict if a record is going to hit the end of the page and if so force a page break. That seems clumsy.
Reading on StackOverflow there are plenty of discussions from 10 years ago that it's a cfdocument bug. But it still doesn't seem to have a solution. One of the hacks seems to work for me, putting a <div> </div><BR> just above the section I don't want to break. The css break-inside doesn't seem to have any effect for me.
d
Fwiw, I tested
page-break-inside: avoid
with
<cfhtmltopdf>
, and it doesn't work there either (at least, not in CF 2016). Wkhtmltopdf-- which I believe
<cfhtmltopdf>
is based on-- does supposedly support
page-break-inside: avoid
, so I'm not sure why CF doesn't. Maybe it's using an older version or something. If you really want good page-break handling with dynamic content, you might want to look into using wkhtmltopdf directly, or another 3rd party tool like WeasyPrint, or even Headless Chrome (I used non-headless Firefox, started via cfexecute, together with a PDF printer, for this exact reason, and it worked great for many years; nothing beats a browser for rendering HTML/CSS).