Dompdf Table Fixed Column Width and Break Long Text

DomPDF table fixed column width and break long text

Not really an "answer", but my solution is switching to Snappy PDF (used the Laravel Package).

Worked like a charm, I did not even change the HTML markups.

dompdf ignores width attribute

Update the last vertion, it should to work,



Automatic column width with `overflow-wrap: break-word`

I think either you should go with display flex or add a small tweak in the CSS like shown below.

.table-wrap tr > td:nth-child(1),
.table-wrap tr > td:nth-child(2) {
white-space: nowrap;

.table-wrap tr > td:nth-child(3) {
word-break: break-word;

th,td {
padding: 0px 16px
<table class="table-wrap" style="width: 100%;">

dompdf create pdf with four columns?

Ok it looks like all I need to do is set the table width to auto or 100% and let the columns scale across the page automatically without setting their widths.

Text overflowing tables when generating PDF with dompdf

I believe DOMPDF is using a fairly limited character set for determining how to split a line. Right now it only splits a line at a dash or a space. So something like the URL you have in your sample is going to run past the width of the container. DOMPDF just doesn't know how to break it up.

Starting with dompdf v0.6.0 you can style your text so that words are broken at any character, e.g.:

<span style="word-wrap: break-word;"></span>

It's not as clean as breaking on a particular character (e.g. a /). If you're comfortable hacking the code you can work around the problem a little more elegantly. Open up the text reflower class and modify the regular expression that splits the line. The regular expression looks like the following:

preg_split('/([\s-]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE)

Modify that code to include whatever extra characters you think will make for a good line break. You might, for example, break URLs up on ?, &, or even / if you expect to have extremely long URLs in your text:

$words = preg_split('/([\s-\?\&\/]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE);

In dompdf 0.6.1 the RegEx can be found in dompdf/include/text_frame_reflower.cls.php lines 86 and 371. In the upcoming 0.7.0 the RegEx can be found in dompdf/src/FrameReflower/Text.php lines 106 and 402.

The drawback to modifying the RegEx is that this will affect all text (not just URLs).

Related Topics

Leave a reply