Computing Magazine

Add Line Break in PDF Generated from HTML Without Fix Width

Posted on the 31 January 2013 by Akahgy

Description:

By creating a PDF from HTML if you use tables and you want the table cells to flow easily not depending on the width of a specific cell, a long text is not broken into multiple lines, but the cell is pushed until the end of the page, destroying the layout or overlapping with another cell.

Solution:

The first solution is to use max-width, which in theory would work, just that most of the PDF converters discard this property. Use of width is also not a solution, as the layout will look bad for a short text inside the cell.

The solution is to first calculate the length of the string, and for a longer one (whatever this means for each requirement) we can add a fix width css class :

<td class=”<?php echo (strlen($title) > 100) ? ‘long_title’ :   ‘short_title’ ?>”>
<?php echo $title; ?>
</td>

Then create the style class .long_title :

.long_title {
width: 250px;
}

 


Back to Featured Articles on Logo Paperblog