PHP + PDF换行符 [英] PHP + PDF Line Break

查看:317
本文介绍了PHP + PDF换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Magento商店中有以下代码,它将客户地址添加到发票PDF中.有时地址的行对于地址标签来说太长了,因此我添加了$ value = wordwrap($ text,10,"
\ n");认为这可以创建一条新线.但是,这似乎在PDF文档中不起作用,而我最后只是在一个有趣的符号上标出了我想要的行.有谁知道我可以换一条新线吗?

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-我的PHP知识非常基础.

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

推荐答案

使用自动换行是一个不错的开始,但并不能帮助您.您可能想做的是为每行单独调用$page->drawText.

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.

例如,像这样的东西.

$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坐标.

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

这篇关于PHP + PDF换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆