不打印FPDF A4尺寸

不打印FPDF A4尺寸

问题描述:

I use FPDF for PDF generation in my project. I want to generate A4 size page and I use following line.

$fpdf = new PDF('P','mm',array(595.28,841.89));
$fpdf->AddPage('P', 'A4'); 

I generate the PDF using this code and I got print out. Then I realize that the page size is not fit for the A4.
Can somebody help me on this regard?

我在项目中使用FPDF生成PDF。 我想要生成A4大小的页面,我使用以下行。 p>

  $ fpdf = new PDF('P','mm'  ,array(595.28,841.89)); 
 $ fpdf-> AddPage('P','A4');  
  code>  pre> 
 
 

我使用此代码生成PDF并打印出来。 然后我意识到页面大小不适合A4。
是否有人在这方面帮助我? p> div>

The problem is that you pass the wrong default page size (595.28mm by 841.89mm) to the FPDF constructor, which by the way should be FDPF. A4 is 210mm by 297mm if in portrait mode.

So you should call the constructor this way:

$pdf = new FPDF('P','mm','A4');

or

$pdf = new FPDF('P','mm',array(210,297));

After that you can omit the page size parameter when calling AddPage().

Here is a complete Hello World example:

<?php
require('fpdf.php');

$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>