Add line break in PDF generated from HTML without fix width

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;
}

 

Advertisements

About admin

Just another php developer trying to give something back to the community.
This entry was posted in Best practices, PhP and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s