Archive for the ‘PHP’ category

My solution to filling Adobe Acrobat forms using shared hosting

July 4th, 2012

In my earlier post I discussed how you can fill in pdf forms via FDF files and send that over to a third party tool like Pdftk. That is all fine and good if you are the admin of your server or you are paying for a private server, but if you are like me and have shared hosting, which I am finding most out there do, you have very limited options, but you do have some options and I am going to show you a little example of what I have done to get it accomplished.

I have attached my snippet of code that will process the HTML form data being sent to your php script as described in my article before.

<?php
require_once('fpdf.php');
require_once('fpdi.php');

//use this if you want to see the posted data
echo'<pre>POST '; print_r($_POST);echo '</pre>';

// initiate FPDI
$pdf = new FPDI();
// add a page
$pdf->AddPage();
// set the sourcefile
$pdf->setSourceFile('test.pdf');
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 0,0 with a width of 200 mm
$pdf->useTemplate($tplIdx, 0 ,0, 200);

//setting font size and color
$pdf->SetFont('Arial');
$pdf->SetFontSize(8);
$pdf->SetTextColor(0,0,0);

//check to see if data is being posted and continue
if(isset($_POST) && is_array($_POST) && count($_POST)){
    
	//fill in First Name 
    	$pdf->SetXY(4, 26);
    	$pdf->Write(0, $_POST['FirstName']);

}else{
    echo 'You did not submit a form.';
	exit;
}

//send the generated pdf to the screen
$pdf->Output('newpdf.pdf', 'D');

?>




Notice that a couple of php files are being referenced at the top, you will need to download the FPDF library found here: FPDF Library and the FPDI library found here: FPDI You will probably need to download both files there to get the correct files. FPDF is only needed because FPDI depends on it.

There is one giant piece of suck to this solution tho, and I can’t find the solution I really want either. The big suck with this is that you are basically positioning the data on top of the pdf via X,Y coordinates, so if you notice around the FirstName spot you see where I am putting it (4,26), so that is 4 over and 26 down on the form. Until I can find a better solution, this is all I got. I would love to find a php library out there that can bind the POST data to the form fields but it doesn’t seem to exist in pure PHP.