PHP + PDF 换行符

发布于 2024-10-16 20:06:07 字数 862 浏览 3 评论 0原文

我的 Magento 商店中有以下代码,它将客户地址添加到发票 PDF 中。有时,地址的行对于地址标签来说太长,因此我添加了 $value = wordwrap($text, 10, "
\n");线路认为这可以创建一条新线路。然而,这似乎在 PDF 文档中不起作用,我最终得到了一个有趣的符号,我希望该线位于该位置。有谁知道我怎样才能获得一条新线路?

PS - 我的 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;
}
}
}
} 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紙鸢 2024-10-23 20:06:07

使用自动换行是一个好的开始,但它不会让您一路顺利。您可能想要做的是为每一行单独调用 $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 中的位置,它可能会变得相当复杂。例如,如果用户可以在该部分中输入任意数量的文本,那么您还需要确保该文本不会溢出到其他部分的文本中。我的意思是,如果这个文本块位于另一个文本块之上,则随着 wordwrap() 创建的行数增加,您需要下推下部块的 y 坐标

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

不乱于心 2024-10-23 20:06:07

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')->str_split 更改为 Mage::helper('core/string')->splitWords``

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``

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文