PHP + PDF换行符

时间:2011-02-09 21:25:58

标签: php pdf magento

我在Magento商店中有以下代码,它将客户地址添加到发票PDF中。有时地址行的地址对于地址标签来说太长了所以我添加了$ value = wordwrap($ text,10,“
\ n”);认为这可能会创造一个新的路线。然而,这似乎不适用于PDF文档,我最终得到一个有趣的符号,我喜欢这条线。有谁知道我怎么能换新线?

P.S - 我的PHP知识非常基础。

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

2 个答案:

答案 0 :(得分:7)

使用wordwrap是一个很好的开始,但它不会让你一路走来。您可能想要做的是为每一行单独调用$page->drawText

例如,像这样的东西。

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

请注意,根据您在pdf上的位置,它会变得相当复杂。例如,如果用户可以在此部分中输入尽可能多的文本,则还需要确保此文本不会溢出到另一部分的文本中。我的意思是,如果你在另一个文本块的上面有这个文本块,你需要按下word块()增加的行数来下推下一个块的y坐标

答案 1 :(得分:0)

Magento 1.7

代替(在app / code / local / Mage / Sales / Model / Order / Pdf / Abstract.php中的第415行,如果此路径上没有文件,请从app / code / core / Mage / Sales复制它。 。位置)

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

使用此

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

还将Mage :: helper('core / string') - &gt; str_split更改为Mage :: helper('core / string') - &gt; splitWords``

相关问题