ADD custom data to PDF using FPDF & FPDI

home_blogger2_lifestyle1
Vestibulum commodo volutpat laoreet
May 8, 2014

Client Requirement

Customer would like the original PDF document used as a template but text fields layered on top of the PDF where the yellow highlighted fields are.

Original PDF

Result PDF

&lt;?php require_once('fpdf.php'); require_once('fpdi.php'); $pdf = new FPDI(); // get the page count $pageCount = $pdf-&gt;setSourceFile('Digital-Ants.pdf');<br />
// iterate through all pages<br />
for ($pageNo = 1; $pageNo &lt;= $pageCount; $pageNo++) { // import a page $templateId = $pdf-&gt;importPage($pageNo);<br />
    // get the size of the imported page<br />
    $size = $pdf-&gt;getTemplateSize($templateId);</p>
<p>    // create a page (landscape or portrait depending on the imported page size)<br />
    if ($size['w'] &gt; $size['h']) {<br />
        $pdf-&gt;AddPage('L', array($size['w'], $size['h']));<br />
    } else {<br />
        $pdf-&gt;AddPage('P', array($size['w'], $size['h']));<br />
    }</p>
<p>    // use the imported page</p>
<p>   if($pageNo == 1)<br />
   {<br />
	    $pdf-&gt;useTemplate($templateId);</p>
<p>   $pdf-&gt;SetFont('Arial','',7);<br />
   $pdf-&gt;SetTextColor(0,0,0);<br />
    $pdf-&gt;SetXY(10, 73);<br />
  $pdf-&gt;Write(4,'Business Name');<br />
   $pdf-&gt;SetXY(128, 70);<br />
  $pdf-&gt;Write(4,'Contact Name');<br />
  $pdf-&gt;SetXY(10, 83);<br />
  $pdf-&gt;Write(4,'Business Name(DBA)');<br />
  $pdf-&gt;SetXY(128, 79);<br />
  $pdf-&gt;Write(4,'Email address');<br />
  $pdf-&gt;SetXY(42, 88);<br />
  $pdf-&gt;Write(4,'Business Location Address');<br />
  $pdf-&gt;SetXY(109, 91);<br />
  $pdf-&gt;Write(4,'Business Billing Address');<br />
  $pdf-&gt;SetXY(38, 96);<br />
  $pdf-&gt;Write(4,'City, State, Zip ');<br />
  $pdf-&gt;SetXY(24, 104.5);<br />
  $pdf-&gt;Write(4,'Phone#');<br />
  $pdf-&gt;SetXY(69, 104.5);<br />
  $pdf-&gt;Write(4,'Fax#');<br />
  //$pdf-&gt;SetXY(10, 118);<br />
  //$pdf-&gt;Write(4,'Ownership');<br />
  $pdf-&gt;SetXY(10, 123.5);<br />
  $pdf-&gt;Write(4,'Owner/Officer/Principal Name');<br />
  $pdf-&gt;SetXY(81, 123.5);<br />
  $pdf-&gt;Write(4,'Title');<br />
  $pdf-&gt;SetXY(118, 123.5);<br />
  $pdf-&gt;Write(4,'DOB');<br />
  $pdf-&gt;SetXY(139, 123.5);<br />
  $pdf-&gt;Write(4,'SSN');<br />
  $pdf-&gt;SetXY(171, 123.5);<br />
  $pdf-&gt;Write(4,'Faderal Tax ID');<br />
  $pdf-&gt;SetXY(10, 131);<br />
  $pdf-&gt;Write(4,'Home Address');<br />
  $pdf-&gt;SetXY(102, 131);<br />
  $pdf-&gt;Write(4,'City, State, Zip');<br />
  $pdf-&gt;SetXY(171.5, 131);<br />
  $pdf-&gt;Write(4,'Phone#');<br />
  $pdf-&gt;SetXY(13, 216);<br />
  $pdf-&gt;Write(4,'Signature');<br />
  $pdf-&gt;SetXY(88, 216);<br />
  $pdf-&gt;Write(4,'Name');<br />
  $pdf-&gt;SetXY(180, 216);<br />
  $pdf-&gt;Write(4,'Date');<br />
   }<br />
}</p>
<p>// Output the new PDF<br />
$pdf-&gt;Output();  

Leave a Reply

Your email address will not be published. Required fields are marked *