douhe5092 2014-06-12 03:56
浏览 43
已采纳

如何使用FPDF在PHP中打印PDF。

I'm trying to print an invoice in PHP using the FPDF library. I can get it to display line items, but it's never in the right format. I tried to change the X and Y positions but I can't get it to do what I want. I would like to display the logo and address on the left side, and the invoice number and order data on the right side of the document.

Here is the code I have so far:

require('includes/fpdf/fpdf.php');

//Create a new PDF file
$pdf=new FPDF();
$pdf->AddPage();


//Fields Name position
$Y_Fields_Name_position = 20;
//Table position, under Fields Name
$Y_Table_Position = 26;


//First create each Field Name
//Gray color filling each Field Name box
$pdf->SetFillColor(232,232,232);
//Bold Font for Field Name
$pdf->SetFont('Arial','B',12);
$pdf->SetY($Y_Fields_Name_position);
$pdf->SetX(5);
$pdf->Cell(25,6,'Order Date',1,0,'L',1);


$pdf->SetX(30);
$pdf->Cell(50,6,'Graphixide Order No',1,0,'L',1);


$pdf->SetX(75);
$pdf->Cell(40,6,'Artwork',1,0,'L',1);


$pdf->SetX(110);
$pdf->Cell(20,6,'PO #',1,0,'L',1);


$pdf->SetX(130);
$pdf->Cell(20,6,'Quantity',1,0,'L',1);


$pdf->SetX(150);
$pdf->Cell(25,6,'Amount',1,0,'L',1);


$pdf->SetX(175);
$pdf->Cell(25,6,'Comments',1,0,'L',1);




//Now show the 3 columns
$pdf->SetFont('Arial','',12);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(5);
$pdf->MultiCell(25,6,$column_order_date,1);


$pdf->SetY($Y_Table_Position);
$pdf->SetX(30);
$pdf->MultiCell(80,6,$column_order_no,1);


$pdf->SetY($Y_Table_Position);
$pdf->SetX(75);
$pdf->MultiCell(40,6,$column_artwork,1);




$pdf->SetY($Y_Table_Position);
$pdf->SetX(110);
$pdf->MultiCell(20,9,$column_cpo,1);


$pdf->SetY($Y_Table_Position);
$pdf->SetX(130);
$pdf->MultiCell(20,6,$column_order_quantity,1);


$pdf->SetY($Y_Table_Position);
$pdf->SetX(150);
$pdf->MultiCell(75,6,$column_order_amount,1);


$pdf->SetY($Y_Table_Position);
$pdf->SetX(175);
$pdf->MultiCell(25,10,$column_comments,1);


$pdf->SetY($Y_Table_Position);
$pdf->SetX(150);
$pdf->MultiCell(130,6,'$ '.$total,1,'R');


//Create lines (boxes) for each ROW (Product)
//If you don't use the following code, you don't create the lines separating each row
$i = 0;




while ($i < $number_of_products)
{
$pdf->SetX(45);
$pdf->MultiCell(120,6,'',1);
$i = $i +1;
}
$pdf->Output();
?>

The output does display, but it doesn't use the proper formatting I want it to. Here is what I'm getting:

enter image description here


I also tried to change the header and footers like this, but for some reason that only displays the logo. Code:

class PDF extends FPDF
{


function Header()
{
    // Logo
    $this->Image('images/logo.png',10,6,30);
    // Arial bold 15
    $this->SetFont('Arial','B',15);
    // Move to the right
    $this->Cell(80);
    // Title
    $this->Cell(30,10,'Title',1,0,'C');
    // Line break
    $this->Ln(20);
}


// Page footer
function Footer()
{
    // Position at 1.5 cm from bottom
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','I',8);
    // Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

展开全部

  • 写回答

1条回答 默认 最新

  • douwan7382 2014-06-12 04:43
    关注

    If you mix Cell and MultiCell, you have to pre-calculate the needed lines/height for each row and reset the X and Y position after a MultiCell call manually because MultiCell will automatically set the position to the beginning of the next line.

    A good example and a method to pre-calculate the needed lines is available in this script.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 mstsc远程桌面轮流掉线
  • ¥15 求一下oivio数据集
  • ¥15 android开发百度网盘文件浏览功能,是什么做的?用的什么技术实现?
  • ¥20 地震波形数据向量化的问题
  • ¥15 脊柱MRI图片分割与自动识别
  • ¥50 使用ADC0809 与 51 单片机设计电路以实现显示电压模拟值
  • ¥15 QGC打开没有地图显示,离线地图也不显示,如何解决?
  • ¥20 Android Studio 的 webview 与访问网络存在的限制
  • ¥15 某帖子的数据集不清楚来源,求帮助
  • ¥15 idea构建mod报错无效的源发行版项目链接,如何解决?
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部