php 如何创建 FPDF 表格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4637645/
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 13:47:34  来源:igfitidea点击:

How to create FPDF tables?

phpfpdf

提问by NORM

I have a problem creating tables with fpdf.

我在使用 fpdf 创建表格时遇到问题。

Can anyone help me making this page (or at least tell me how to do it)?

谁能帮我制作这个页面(或者至少告诉我怎么做)?

Or can you show how to convertHTML tables to FPDF tables and put the code here?

或者您能否展示如何将HTML 表格转换为 FPDF 表格并将代码放在这里?

I also have this (along with the connection to the database):

我也有这个(以及与数据库的连接):

$person = mysql_fetch_array($result);

I would like this to function when added inside the tables with this = (example)

我希望在使用 this =(示例)添加到表格中时能够正常工作

$pdf->Cell(0,260,''.$person["CA"],'C',0 ,1); --- .$person["CA"]

Can anyone help me?

谁能帮我?

回答by Phoenix

Well, FPDF works kinda like a typewriter, in that it has a roving X,Y point which it writes to, which you can manually move around or let it do it for you.

嗯,FPDF 的工作方式有点像打字机,因为它有一个旋转的 X、Y 点,它可以写入,您可以手动移动或让它为您完成。

I don't know if the huge whitespace at the left is required. If so, you'd have to call $this->SetX($coordinate) before each write.

我不知道是否需要左侧的巨大空白。如果是这样,您必须在每次写入之前调用 $this->SetX($coordinate) 。

You should do something like this:

你应该做这样的事情:

class InvoicePDF extends FPDF //Create a new class to contain the header/footer/etc. which extends FPDF
{
    public function Header
    {
    //Header stuff goes here, like the big Invoice
        $this->SetY(10); //SetY 10 units down from the top, experiment with distance to get appropriate distance
        $this->SetFont('Arial','',20); //Set Font to Arial/Helvetica 20 pt font
        $this->SetTextColor(0,0,0); //Set Text Color to Black;
        $this->Cell(0,9,"INVOICE",0,0,'R');  //Write the word INVOICE Right aligned in a box the width of the page, will put it at the far right of the page
     }

     public function Footer
     {
       //any footer stuff goes here
     }

     public function FillHeadInfo($info) //$info would be an array of the stuff to fill the small table at the top
     {
          $this->SetY(0); //reset the Y to the original, since we moved it down to write INVOICE
          $this->SetFont('Arial','',12);
          $this->SetFillColor(224,224,224); //Set background of the cell to be that grey color
          $this->Cell(20,12,"Order #",1,0,'C',true);  //Write a cell 20 wide, 12 high, filled and bordered, with Order # centered inside, last argument 'true' tells it to fill the cell with the color specified
          $this->Cell(20,12,"Coding",1,0,'C',true);
          $this->Cell(20,12,"Sales Code",1,1,'C',true); //the 1 before the 'C' instead of 0 in previous lines tells it to move down by the height of the cell after writing this

          $this->Cell(20,12,$info['ordernum'],1,0,'C');
          $this->Cell(20,12,$info['coding'],1,0,'C');
          $this->Cell(20,12,$info['salescode'],1,1,'C');

          $this->Cell(40,12,"Name & Address",1,0,'C',true);
          $this->Cell(20,12,"Date",1,1,'C',true);
          $y = this->GetY(); //Need the current Y value to reset it after the next line, as multicell automatically moves down after write
          $x = this->GetX(); // Might need the X too
          $this->MultiCell(40,12,$info['customername'] . "\n" . $info['address'] . "\n" . $info['city'] . ', ' . $info['state'] . ' ' . $info['zip'],1,'L',false); //I assume the customer address info is broken up into multiple different pieces
          $this->SetY($y);  //Reset the write point
          $this->SetX($x + 40); //Move X to $x + width of last cell

          $this->Cell(20,36,date("format",strtotime($info['date'])),1,1,'C');  //Might be easier to use $this->Rect() to draw rectangles for address and date and then write the address and date into them without borders using SetX and SetY, if the borders don't line up or whatever

     }

     public function fillItems($items)
     {
          //You'd build the items list much the same way as above, using a foreach loop or whatever
          //Could also easily combine this function and the one above
     }
}

Then, when creating the pdf you'd do something like this:

然后,在创建 pdf 时,您将执行以下操作:

require_once('fpdf.php');
require_once('class.invoicepdf.php');
//Get whatever info you need to fill the pdf
$pdf = new InvoicePDF('P','mm','Letter');
$pdf->AddPage();
$pdf->FillHeadInfo($info);  //Could also pass $_POST and rely on the keys of the $_POST array
$pdf->FillItems($items);
$pdf->Output('filename.pdf','I');

By the way, I'd suggest rather than trying to write it from $_POST, you save the order to the DB, then pass the order ID along to the script to write the pdf via a link and $_GET, and have the script retrieve the info from the DB, this way you can select only the info you need.

顺便说一句,我建议不要尝试从 $_POST 中写入它,而是将订单保存到数据库中,然后将订单 ID 传递给脚本以通过链接和 $_GET 编写 pdf,并使用脚本从数据库中检索信息,这样您就可以只选择您需要的信息。