使用 FPDF/FPDI/TCPDF 生成 PDF 时文本被剪切
我有一个 PHP 项目,我正在打开一个预制的 PDF,并通过 PHP 填写数据。我遇到的问题是其中一个文本元素没有显示。我将其放置在 PDF 页面的右下角。如果我将它向左移动一点,它就会显示出来。就好像有一些剪辑什么的。
我正在使用 TCPDF,并且由于我需要修改现有的 PDF,所以我还必须使用 FPDI 类。在我看来,FPDI 通常与 FPDF 集成,因此我一直使用 FPDF 方法来构建我的 PDF。好的,这是我的一些代码(或相关部分)...
$pdf = new PDF();
$pdf->AddPage( 'L', 'Letter' );
$pdf->SetAutoPageBreak(false);
$pdf->SetXY(261,200);
$pdf->Write(5, 'test');
当我将文本定位为 260
时,文本右侧至少有一厘米的空白。如果我将它再移动一个单位到 261
,就像上面的代码一样,它就会消失。到目前为止,我能够将文本定位在页面底部,仅显示字母的上半部分,但是,我什至无法接近页面的右侧,否则文本将完全消失。我已将 SetAutoPageBreak
设置为 false,因此不会创建新页面,并且我还尝试将边距归零。
I have a PHP project where I am opening up a premade PDF, and filling it out with data via PHP. The problem I am having is that one of the text elements isn't showing up. I am positioning it towards the bottom right of the PDF page. If I move it to the left a little, it shows up. It's as if there is some clipping or something.
I am using TCPDF, and since I am needing to modify an existing PDF, I am also having to use the FPDI class. It appears to me that FPDI normally is integrated with FPDF, so I've been using the FPDF methods to build out my PDF. OK, so here is some of my code (or the relevant parts)...
$pdf = new PDF();
$pdf->AddPage( 'L', 'Letter' );
$pdf->SetAutoPageBreak(false);
$pdf->SetXY(261,200);
$pdf->Write(5, 'test');
There is at least a centimeter of whitespace to the right of the text when I position the text with a value of 260
. If I move it just one more unit to 261
, like in the code above, it just disappears. I'm able to position the text so far on the bottom of the page, that only the top half of the letters show, however, I can't even approach the right side of the page, or the text will completely disappear. I've set the SetAutoPageBreak
to false, so new pages aren't created, and I've also flirted with zeroing out the margins.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
游戏可能有点晚了...
我查看了 fpdf.php 文件,看起来它增加了 1 厘米的边距。
如果降低它,则可以使文本更靠近页面边缘。以下是原始行:
但如果将其更改为类似这样的内容,
这将使您更接近文档的边缘。
Might be a bit late in the game...
I had a look at the fpdf.php file and it looks like it adds a margin of 1cm.
If you lower it then you can get text closer to the edge of the page. Below is the original line:
But if you change it to something like
This gets you closer to the edge of your document.
我很幸运地使用单元格来定位文本,出于某种原因,它们比简单地将文本写入文档更准确且更容易使用:
文档:http://www.fpdf.org/en/doc/cell.htm
I've had the best luck using cells to position text, for some reason they have shown to be more accurate and easier to work with than simply writing text onto the document:
Docs: http://www.fpdf.org/en/doc/cell.htm
Write() 用于流动文本(在内部它使用多个 Cell() 调用)。如果到达右边距,则会自动换行,下一个单词/字符将在左边距开始新行。
该单词不会消失,但会显示在左下区域。
您可以通过这个简单的脚本看到角色的流动:
Write() is used for flowing text (internally it uses several Cell() calls). If it reaches the right margin an automatic line break is done and the next word/character will start a new line at the left margin.
The word will not disappear but will be shown at the lower left area.
You can see the characters flowing with this simple script:
请尝试以下操作:
Try the following: