PHP + PDF Line Break

2020-04-10 11:11发布

问题:

I have the below code in my Magento store it adds the customers address to the invoice PDF's. Sometimes the lines of the address would be too long for the address labels so I added the $value = wordwrap($text, 10, "
\n"); line thinking this could create a new line. However, this doesn't seem to work in PDF docs and i just end up with a funny symbol where I'd like the line to be. does anyone know how I can get a new line?

P.S - My PHP knowledge is very basic.

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

回答1:

Using wordwrap is a good start, but it won't get you all the way there. What you will likely want to do is do 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 be aware that depending on where you have this on the pdf, it can get quite complex. For example, if the user can enter in as much text as they want into this section, you will also need to make sure that this text doesn't overrun into another section's text. By this I mean if you have this block of text just above another block of text, you need to push down the y-coordinates of the lower block as the number of lines created by wordwrap() increases



回答2:

Magento 1.7

instead (around line 415 in app/code/local/Mage/Sales/Model/Order/Pdf/Abstract.php if dont have file on this path, copy it from app/code/core/Mage/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 to Mage::helper('core/string')->splitWords``



标签: php pdf magento