问题描述
我有一个生成PDF文件的简单脚本.我从数据库中获取所需的信息,并使用foreach创建PDF文件的页面,最后一页我有一些图表和其他未从DB检索的图表和其他信息.除最后一个外,所有页面都必须具有一个子标题.
我当前的代码是:
foreach ($sql as $key => $value) { $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L'); $pdf->Cell(90, $line_height, $value['product'], '', '', 'L'); $pdf->Cell(90, $line_height, $value['ammount'], '', '', 'L'); $pdf->Cell(90, $line_height, $value['value'], '', '', 'L'); }
基本上,我需要让子标题描述每一行.但是,如果我这样做,所有行都将具有子标题:
foreach ($sql as $key => $value) { $pdf->SetFont('Arial', 'B', $font_size); $pdf->Ln(2 * $line_height); $pdf->Cell(20, $line_height, 'Number', '', '', 'L'); $pdf->Cell(120, $line_height, 'Product', '', '', 'L'); $pdf->Cell(40, $line_height, 'Ammount', '', '', 'L'); $pdf->Cell(40, $line_height, 'Value ($)', '', '', 'L'); // $pdf->SetFont('Arial', 'B', $font_size); $pdf->Ln(2 * $line_height); $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L'); $pdf->Cell(120, $line_height, $value['product'], '', '', 'L'); $pdf->Cell(40, $line_height, $value['ammount'], '', '', 'L'); $pdf->Cell(40, $line_height, $value['value'], '', '', 'L'); }
我需要每个页面中的这个子标题,除了最后一个.我尝试使用pageNo()函数进行一些疯狂的代码,但它不起作用.
谢谢!
推荐答案
好的,我解决了我的问题.对于任何正在寻找类似事物的人,这就是我所做的:GetY()函数返回该行的位置,因此当它是页面的第一行时,该值将为0(或一个常数,取决于您布局).我只是这样做了:
foreach ($sql as $key => $value) { if ($pdf->GetY() == 0) { $pdf->SetFont('Arial', 'B', $font_size); $pdf->Ln(2 * $line_height); $pdf->Cell(20, $line_height, 'Number', '', '', 'L'); $pdf->Cell(120, $line_height, 'Product', '', '', 'L'); $pdf->Cell(40, $line_height, 'Ammount', '', '', 'L'); $pdf->Cell(40, $line_height, 'Value ($)', '', '', 'L'); } $pdf->SetFont('Arial', 'B', $font_size); $pdf->Ln(2 * $line_height); $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L'); $pdf->Cell(120, $line_height, $value['product'], '', '', 'L'); $pdf->Cell(40, $line_height, $value['ammount'], '', '', 'L'); $pdf->Cell(40, $line_height, $value['value'], '', '', 'L'); }
谢谢大家!
其他推荐答案
覆盖 header() fpdf 在您的同类中.
class PDF extends FPDF { public function Header() { $this->Cell(0, 8, "HEADER TEXT", 0, 0, 'R'); } }
此方法将在添加新页面时自动称为 .
其他推荐答案
我也有同样的问题,试图在每个页面上获取标头.这也连接到MySQL数据库.这是最终为我工作的原因.
<?php //PDF USING MULTIPLE PAGES //FILE Originally CREATED BY: Carlos José Vásquez Sáez //I fixed this on May 3/2013 as it was not working correctly for me. define('FPDF_FONTPATH', '/font/'); require('fpdf.php'); //Connect to your database $link = mysql_connect("localhost","","") or die ('Could not select database.'); $db_select = mysql_select_db("", $link) or die ('Could not select database.'); //Create new pdf file $pdf=new FPDF(); //Open file $pdf->Open(); //Disable automatic page break $pdf->SetAutoPageBreak(false); //Add first page $pdf->AddPage(); //set initial y axis position per page $y_axis_initial = 10; //Set Row Height $row_height = 8; //print column titles for the actual page $pdf->SetFillColor(232, 232, 232); $pdf->SetFont('Arial', 'B', 11); $pdf->SetY($y_axis_initial); $pdf->SetX(10); $pdf->Cell(80, $row_height, 'Products', 1, 0, 'C', 1); $pdf->Cell(20, $row_height, 'Price', 1, 0, 'C', 1); $pdf->Cell(30, $row_height, 'Customer', 1, 0, 'C', 1); //Select the Products you want to show in your PDF file $result=mysql_query('SELECT Product,Price,Customer FROM your_table ORDER BY Product', $link); //initialize counter $i = 0; //Set maximum rows per page $max = 30; //Data Table y axis position starting point $y_axis = $y_axis_initial + $row_height; while($row = mysql_fetch_array($result)) { //If the current row is the last one, create new page and print column title if ($i == $max) { $pdf->AddPage(); //print column titles for the current page $pdf->SetY($y_axis_initial); $pdf->SetX(10); $pdf->Cell(80, $row_height, 'Product', 1, 0, 'C', 1); $pdf->Cell(20, $row_height, 'Price', 1, 0, 'C', 1); $pdf->Cell(30, $row_height, 'Customer', 1, 0, 'C', 1); //Set $i variable to 0 (first row) $i = 0; //Reset the y axis value $y_axis = $y_axis_initial + $row_height;; } $Product = $row['Product']; $Price = $row['Price']; $Cust = $row['Customer']; $pdf->SetY($y_axis); $pdf->SetX(10); $pdf->Cell(80, $row_height, $Product, 1, 0, 'L', 1); $pdf->Cell(20, $row_height, $Price, 1, 0, 'L', 1); $pdf->Cell(30, $row_height, $Cust, 1, 0, 'L', 1); //Go to next row $y_axis = $y_axis + $row_height; $i = $i + 1; } mysql_close($link); //Create file $pdf->Output();
问题描述
I have a simple script that generates a PDF file. I'm getting the info I need from the database and using a foreach to create the pages of the PDF file, and the last page I have some charts and other info that are not retrived from DB. All the pages, except this last one, must have a subheader.
My current code is:
foreach ($sql as $key => $value) { $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L'); $pdf->Cell(90, $line_height, $value['product'], '', '', 'L'); $pdf->Cell(90, $line_height, $value['ammount'], '', '', 'L'); $pdf->Cell(90, $line_height, $value['value'], '', '', 'L'); }
Basically, I need to have the subheader describing each row. But If I do this, all the lines will have the subheader:
foreach ($sql as $key => $value) { $pdf->SetFont('Arial', 'B', $font_size); $pdf->Ln(2 * $line_height); $pdf->Cell(20, $line_height, 'Number', '', '', 'L'); $pdf->Cell(120, $line_height, 'Product', '', '', 'L'); $pdf->Cell(40, $line_height, 'Ammount', '', '', 'L'); $pdf->Cell(40, $line_height, 'Value ($)', '', '', 'L'); // $pdf->SetFont('Arial', 'B', $font_size); $pdf->Ln(2 * $line_height); $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L'); $pdf->Cell(120, $line_height, $value['product'], '', '', 'L'); $pdf->Cell(40, $line_height, $value['ammount'], '', '', 'L'); $pdf->Cell(40, $line_height, $value['value'], '', '', 'L'); }
I need this subheader in each page, except the last one. I've tried to do some crazy code with pageNo() function, but it didn't work.
Thank's!
推荐答案
Ok guys, I solved my problem. For anyone who's looking for something similar, here is what I did: The GetY() function return the Y position of that line, so when it's the first line of the page, this value will be 0 (or a constant, depends of your layout). I just did:
foreach ($sql as $key => $value) { if ($pdf->GetY() == 0) { $pdf->SetFont('Arial', 'B', $font_size); $pdf->Ln(2 * $line_height); $pdf->Cell(20, $line_height, 'Number', '', '', 'L'); $pdf->Cell(120, $line_height, 'Product', '', '', 'L'); $pdf->Cell(40, $line_height, 'Ammount', '', '', 'L'); $pdf->Cell(40, $line_height, 'Value ($)', '', '', 'L'); } $pdf->SetFont('Arial', 'B', $font_size); $pdf->Ln(2 * $line_height); $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L'); $pdf->Cell(120, $line_height, $value['product'], '', '', 'L'); $pdf->Cell(40, $line_height, $value['ammount'], '', '', 'L'); $pdf->Cell(40, $line_height, $value['value'], '', '', 'L'); }
Thank you all!
其他推荐答案
override the method header() of FPDF in your class.
class PDF extends FPDF { public function Header() { $this->Cell(0, 8, "HEADER TEXT", 0, 0, 'R'); } }
This method will be called automatically by the FPDF while adding a new page.
其他推荐答案
I had this same problem trying to get a header on each page. This also connects to a MySql database. Here is what ended up working for me.
<?php //PDF USING MULTIPLE PAGES //FILE Originally CREATED BY: Carlos José Vásquez Sáez //I fixed this on May 3/2013 as it was not working correctly for me. define('FPDF_FONTPATH', '/font/'); require('fpdf.php'); //Connect to your database $link = mysql_connect("localhost","","") or die ('Could not select database.'); $db_select = mysql_select_db("", $link) or die ('Could not select database.'); //Create new pdf file $pdf=new FPDF(); //Open file $pdf->Open(); //Disable automatic page break $pdf->SetAutoPageBreak(false); //Add first page $pdf->AddPage(); //set initial y axis position per page $y_axis_initial = 10; //Set Row Height $row_height = 8; //print column titles for the actual page $pdf->SetFillColor(232, 232, 232); $pdf->SetFont('Arial', 'B', 11); $pdf->SetY($y_axis_initial); $pdf->SetX(10); $pdf->Cell(80, $row_height, 'Products', 1, 0, 'C', 1); $pdf->Cell(20, $row_height, 'Price', 1, 0, 'C', 1); $pdf->Cell(30, $row_height, 'Customer', 1, 0, 'C', 1); //Select the Products you want to show in your PDF file $result=mysql_query('SELECT Product,Price,Customer FROM your_table ORDER BY Product', $link); //initialize counter $i = 0; //Set maximum rows per page $max = 30; //Data Table y axis position starting point $y_axis = $y_axis_initial + $row_height; while($row = mysql_fetch_array($result)) { //If the current row is the last one, create new page and print column title if ($i == $max) { $pdf->AddPage(); //print column titles for the current page $pdf->SetY($y_axis_initial); $pdf->SetX(10); $pdf->Cell(80, $row_height, 'Product', 1, 0, 'C', 1); $pdf->Cell(20, $row_height, 'Price', 1, 0, 'C', 1); $pdf->Cell(30, $row_height, 'Customer', 1, 0, 'C', 1); //Set $i variable to 0 (first row) $i = 0; //Reset the y axis value $y_axis = $y_axis_initial + $row_height;; } $Product = $row['Product']; $Price = $row['Price']; $Cust = $row['Customer']; $pdf->SetY($y_axis); $pdf->SetX(10); $pdf->Cell(80, $row_height, $Product, 1, 0, 'L', 1); $pdf->Cell(20, $row_height, $Price, 1, 0, 'L', 1); $pdf->Cell(30, $row_height, $Cust, 1, 0, 'L', 1); //Go to next row $y_axis = $y_axis + $row_height; $i = $i + 1; } mysql_close($link); //Create file $pdf->Output();