Reputation: 31
i want to use dompdf to create html to pdf. I have an design with two columns. Every column could be higher than a normal dina4 page. So i have a page-break.
I dont want to use float, because dompdf float is buggy sometimes and i dont want to float something, i want two fix containers (left, right).
If i use position:absolute; this container cant make a page-break. With position:fixed; this container is on every dina4 site (nice for footer and header, bad for my solution).
If i use a table with two columns to design it, dompdf can not handle table in table and i get an error message. But i have to use a table for my content.
Are there any other Solutions for this Problem? postion:absolute with a correct page break or something else?
EDIT:
My Page: div header (position: fixed;) div footer (position: fixed;) div content (2 column design - left & right with page breaks)
Best Regards
Upvotes: 3
Views: 17519
Reputation: 1
As mentioned above the easiest way is to split the text and put it into a tablestructure. But to do so and fill it up like the css "column-count" attribute would do in normal HTML, a few things might be helpful, as I found out doing the same thing. This is what I ended up.
First try to find the most longest text that still fits in the structure (i.e. column-count). Count the chars (i.e. with a simple texteditor - it won't do anything but to get a figure for the splitting procedure).
With that you can write your code.
Before splitting the text maybe replace any html tags not needed (i.e. double
or so). Then count all <br> and with that number, count with strlen and substract 3 chars for each <br> (this is useful for a <br> will be only one character visible but counting 4).
With all this fill up your string with white spaces as long as it does not exceed you max string length.
Last step: only wordwrapping your text and put it into your table cols.Example for 3 cols below:
$maxlength = 3000; //this is the max amount storable in our 3 columns
$columns = 3; //just makes it easier to change if needed
$columntext = str_ireplace(array("<p><br></p>","<p> </p>"),array("<br>",""),$columntext); //replace all tag-structures that might bring to much linebreaks into the text
$columntext = str_ireplace("\$br\$","<br>",preg_replace('#<[^>]+>#', ' ', str_ireplace(array("<br>","</p>"),"\$br\$",$columntext))); //replace all other tags
$columntext = preg_replace( "/\r|\n/", " ", $columntext); //replace non-html linebreaks with a single whitespace
//now if the text does not exeed its limit, fill up with whitespaces
$brcount = 0;
$textlen = strlen($columntext)-($brcount*3);
//don't use php build in str_pad as it would count <br> as 4 chars instead of used one (the visible one)
while ($textlen<$maxlength)
{
$columntext .=" ";
$textlen++;
}
$parts = wordwrap($columntext,$maxlength/$columns,"</td>");
$parts = "<td>".str_ireplace("</td>","</td><td>",$parts);
//one way of outputting but you sure could use some other ways
echo "<table style=\"width: 99%;\"><tr>";
echo $parts;
echo "</tr></table>";
Upvotes: 0
Reputation: 99
You could use position relative & position absolute with left margins. This works on a single page so should work over more than one page.
<div style="clear:both; position:relative;">
<div style="position:absolute; left:0pt; width:192pt;">
LEFT COLUMN
</div>
<div style="margin-left:200pt;">
RIGHT COLUMN
</div>
</div>
Upvotes: 9
Reputation: 8573
I've ran in to this problem before, (column layout split over more than one page), and with DomPdf the easiest way to get column layouts is to use tables to set out the page, and to do some kind of calc on when to create new rows and columns. This did work for me, but it's not an ideal solution, as you end up having to create a whole new layout plus complicated logic for the pdf.
If I had to do this again, I'd look at http://code.google.com/p/wkhtmltopdf/ which is highly recommended. There's a PHP extension for it... https://github.com/mreiferson/php-wkhtmltox
Here's a review... http://ciaranmcnulty.com/blog/2009/04/converting-html-to-pdf-using-wkhtmltopdf
Please let me know how you get on with this.
Good luck :)
Upvotes: 2