Magento: PHP to PDF Invoice - Create new line / Wordwrap Content

Magento: PHP to PDF Invoice - Create new line / Wordwrap Content

August 25, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: PHP to PDF Invoice - Create new line / Wordwrap Content

Solving Wordwrap Issues in Magento Invoice PDF Generation

As you’re customizing your invoices within the /Mage/Sales/Model/Order/Pdf/Items/Invoice/Default.php file, you’ve encountered a layout challenge: descriptions or attribute values are spilling over into neighboring columns due to insufficient space and width limitations. This occurs because the default str_split function doesn’t inherently handle word wrapping.

To address this issue effectively, we’ll need to modify your existing code to implement proper line breaking for long text content while maintaining column integrity in the PDF layout.

Understanding the Current Challenge

Your current setup uses the str_split helper function with a specified maximum length and wraps long strings across multiple arrays. However, it lacks built-in capabilities to control the width of individual columns or enforce word wrapping based on available space.

In your specific case, you’re fetching and displaying product attributes such as manufacturer and color, which may contain variable-length text that overlaps when combined with other information like product names.

Introducing PHP’s wordwrap Function

To overcome this challenge, we can leverage PHP’s built-in wordwrap() function. This function allows us to specify the maximum length of a string before word wrapping kicks in and automatically wraps words at appropriate points based on available space.

Here’s how you can apply it to your code:

Modifying the Code for Word Wrapping

Replace this section in your file:

$lines[0] = array(array(
    'text' => Mage::helper('core/string')->str_split($item->getName(), 60, true, true),
    'feed' => 35,
));

With the following updated code that includes wordwrap:

// draw Product name with word wrapping
$lines[0] = array(array(
    'text' => Mage::helper('core/string')->wordwrap($item->getName(), 60, "\n", true),
    'feed' => 35,
));

Similarly, update the lines for manufacturer and color attributes:

// draw Brand (Added by James) with word wrapping
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $this->getSku($item), array('manufacturer'));

if ($product) {
    $lines[0][] = array(
        'text' => Mage::helper('core/string')->wordwrap($product->getAttributeText('manufacturer'), 15, "\n"),
        'feed' => 220
    );
}

// draw Colour (Added by James) with word wrapping
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $this->getSku($item), array('pos_short_colour'));

if ($product) {
    $lines[0][] = array(
        'text' => Mage::helper('core/string')->wordwrap($product->getAttributeText('pos_short_colour'), 15, "\n"),
        'feed' => 320
    );
}

Conclusion

By incorporating the wordwrap function into your code, you’re now able to limit the width of each column and ensure that long text content gets automatically broken into new lines based on the specified maximum length.

This approach maintains the integrity of your invoice layout while accommodating variable-length attribute values without causing overlaps. The updated code provides a clean, efficient solution for handling word wrapping in Magento PDF generation.

Remember, it’s essential to thoroughly test these changes in various scenarios and with different product combinations to ensure optimal results across all cases.

For further guidance on optimizing your Magento extensions and customizations, consider exploring the resources available at Laravel Company.

Catalog Themes Orders Errors

Our network