PHP + PDF Line Break

I have the code below in my Magento store that adds the customer address to the PDF invoice file. Sometimes the address lines will be too long for address labels, so I added the value $ value = wordwrap ($ text, 10, "
\ n "); this can create a new line. However, it does not seem to work in PDF documents, and I just get a funny character where I would like the line to be. Does anyone know how I can get a new line?

PS - My knowledge of PHP is very simple.

if (!$order->getIsVirtual()) { if ($this->y < 250) { $page = $this->newPage(); } $this->_setFontRegular($page, 6); $page->drawText('Ship to:', 75, 222 , 'UTF-8'); $shippingAddress = $this->_formatAddress($order->getShippingAddress()->format('pdf')); $line = 185; $this->_setFontRegular($page, 12); $num_lines = count($shippingAddress); $curr_line = 0; foreach ($shippingAddress as $value) { $curr_line += 1; if ($curr_line < $num_lines) { if ($value!=='') { $value = wordwrap($value, 20, "\n"); $page->drawText(strip_tags(ltrim($value)), 75, $line, 'UTF-8'); $line -=14; } } } } 
+3
source share
2 answers

Using wordwrap is a good start, but it will not help you to the end. What you most likely want to do is make a separate call to $page->drawText for each line.

So, for example, something like this.

 $textChunk = wordwrap($value, 20, "\n"); foreach(explode("\n", $textChunk) as $textLine){ if ($textLine!=='') { $page->drawText(strip_tags(ltrim($textLine)), 75, $line, 'UTF-8'); $line -=14; } } 

And keep in mind that depending on where you have it in pdf, it can become quite complicated. For example, if the user can enter as much text as he wants in this section, you also need to make sure that this text is not overflowing with the text of another section. By this I mean that if you have this block of text just above another block of text, you need to press the y-coordinates of the bottom block, as the number of lines created with wordwrap () increases

+7
source

Magento 1.7

instead (line 415 in the application / code / local / Magician / Sales / Model / Order / Pdf / Abstract.php, if you do not have a file on this path, copy it from the application / code / kernel / Magician / Sales. location )

 foreach ($payment as $value){ if (trim($value) != '') { //Printing "Payment Method" lines $value = preg_replace('/<br[^>]*>/i', "\n", $value); foreach (Mage::helper('core/string')->str_split($value, 50, true, true, "\n") as $_value) { $page->drawText(strip_tags(trim($_value)), $paymentLeft, $yPayments, 'UTF-8'); $yPayments -= 15; } } } 

use this

 foreach ($payment as $value){ if (trim($value) != '') { //Printing "Payment Method" lines $value = preg_replace('/<br[^>]*>/i', "\n", $value); foreach (Mage::helper('core/string')->splitWords($value, false,false, "\n") as $_value) { $page->drawText(strip_tags(trim($_value)), $paymentLeft, $yPayments, 'UTF-8'); $yPayments -= 15; } } } 

also change Mage :: helper ('core / string') → str_split for Mage :: helper ('core / string') → splitWords``

0
source

All Articles