php 使用 fpdf 在 pdf 中定位单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19817686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:06:05  来源:igfitidea点击:

positioning the cell in pdf using fpdf

phpfpdf

提问by user2636368

I am trying to make a pdf from the data entered by the user in a form. I am using fpdf to do this. As of now I have something like this for adding the data to the pdf -

我正在尝试根据用户在表单中输入的数据制作 pdf。我正在使用 fpdf 来做到这一点。到目前为止,我有类似的东西可以将数据添加到 pdf -

$pdf->Cell(40,200,'Descritpion');
$pdf->Cell(150,200,$_POST['element_1']);
$pdf->Cell(40,400,'Descritpion2');
$pdf->Cell(150,400,$_POST['element_2']);

This does work but what I want to know is how can I add these to the pdf without specifying the location. As you can see in the above code I am mentioning where the data should be in the pdf but I want to know if there is a way to do it without specifying them. i.e the description1 and element_1 should be on the first few lines and description2 should start of where element_1 ends.

这确实有效,但我想知道的是如何在不指定位置的情况下将这些添加到 pdf 中。正如您在上面的代码中所看到的,我提到了数据应该在 pdf 中的位置,但我想知道是否有办法在不指定它们的情况下做到这一点。即 description1 和 element_1 应该在前几行,而 description2 应该从 element_1 结束的地方开始。

回答by bassxzero

"As you can see in the above code Im mentioning where the data should be in the pdf" this is incorrect. the width 40 and the height 400 are the width and height of the Cell you are creating not the location on the pdf. If you want to set the location of the cell on the pdf you need to use SetX() and SetY() or SetXY() before you create a cell.Example

“正如您在上面的代码中看到的,我提到数据应该在 pdf 中的位置”这是不正确的。宽度 40 和高度 400 是您创建的单元格的宽度和高度,而不是 pdf 上的位置。如果要在 pdf 上设置单元格的位置,则需要在创建单元格之前使用 SetX() 和 SetY() 或 SetXY()。示例

$pdf -> SetY(5);    // set the cursor at Y position 5
$pdf -> SetFont('Arial', 'I', 8);  // set the font
$pdf->Cell(40,200,'Descritpion');  // draw a cell at pos 5 that has a a width 40 and height 400