Ashesh
Ashesh

Reputation: 949

Trying to write to a PDF file using FPDF and FPDI

I was given a PDF file with spots that needed to be filled out with values from the database. I'm using FPDF library along with FPDI, and it seems like a lot of trial and error to position your text...I had to play around with the X and Y coordinates to get the text in correct spots. Seems like a cumbersome and inefficient way to do it. Am I missing something here ?

 require_once('fpdf/fpdf.php');
 require_once('fpdi/fpdi.php');

 // initiate FPDI  
 $pdf = new FPDI();  
 // add a page
 $pdf->AddPage();  
 // set the sourcefile  
 $pdf->setSourceFile('1.pdf');  
 // import page 1  
 $tplIdx = $pdf->importPage(1);  
 // use the imported page and place it at point 10,10 with a width of 200 mm   (This is    the image of the included pdf)
 $pdf->useTemplate($tplIdx, 10, 10, 200);  
 // now write some text above the imported page
 $pdf->SetTextColor(0,0,0);

 $pdf->SetFont('Arial','B',20);  
 $pdf->SetXY(85, 50);  
 $pdf->Write(0, "Johnny Walker");
 $pdf->SetFont('Arial','B',11);
 $pdf->SetXY(92, 69); 
 $pdf->Write(0, "3"); 
 $pdf->SetFont('Arial','B',10);
 $pdf->SetXY(114, 76); 
 $pdf->Write(0, 'Ventilation Management of the Amyotropic Lateral Scleropsis'); 
 $pdf->SetFont('Arial','B',10);
 $pdf->SetXY(114, 90); 
 $pdf->Write(0, date('m/d/Y'));  
 $pdf->SetFont('Arial','B',7);
 $pdf->SetXY(66, 127); 
 $pdf->Write(0, '1.5');  
 $pdf->Output('1.pdf', 'I');

Upvotes: 2

Views: 8732

Answers (1)

mavili
mavili

Reputation: 3424

I have used FPDF in the past once and you're right setting XY positions are quite cumbersome. But there doesn't seem to be another way.

The only thing I would suggest is consider

$pdf->SetXY($pdf->GetX() + $x_value, $pdf->GetY() +  $y_value) 

at places where you aren't sure of the exact position you should place at.

Upvotes: 6

Related Questions