54

I'd like to be able to write/overlay text over an existing pdf document using PHP. What I am hoping to do is have a pdf document that can act as a template, and fill in the gaps by opening the template doc, overlaying the relevant text, and serving the result as a new document. The template document is a single page so page merging/manipulation is not necessary.

Are there any free libraries that can do this? Anywhere I should look? Most searches I've done seem to deal with merging documents/adding pages, instead of overlaying content over an existing page.

Thanks.

*EDIT: Here is what I did: 1. Download FPDF 2. Download FPDI + FPDF_TPL from

http://www.setasign.de/products/pdf-php-solutions/fpdi/downloads/

Here is some sample code for any future wanderers (adapted from the samples at www.setasign.de):

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

// initiate FPDI 
$pdf =& new FPDI(); 
// add a page 
$pdf->AddPage(); 
// set the sourcefile 
$pdf->setSourceFile('templatedoc.pdf'); 
// import page 1 
$tplIdx = $pdf->importPage(1); 
// use the imported page as the template 
$pdf->useTemplate($tplIdx, 0, 0); 

// now write some text above the imported page 
$pdf->SetFont('Arial'); 
$pdf->SetTextColor(255,0,0); 
$pdf->SetXY(25, 25); 
$pdf->Write(0, "This is just a simple text"); 

$pdf->Output('newpdf.pdf', 'D'); 
?>
filip-fku
  • 3,265
  • 1
  • 21
  • 19
  • 2
    Glad you edited in an example of the answer, +1 for that, easy isn't it? :-) – Orbling Nov 29 '10 at 02:13
  • Yes! And it will help me too when in a few weeks I forget what I've done and look for it again :P (Haven't implemented it yet for my project, just found out how to). – filip-fku Dec 02 '10 at 03:14
  • did you find any way to append image in existing document instead of writing text? – M_A_K Oct 15 '14 at 06:52

3 Answers3

24

Have a look at the FPDI Library an add on to FPDF for template annotation.

It can also bolt-on to TCPDF, another popular PHP PDF library. An existing PDF is used as the base of a page, instead of a blank, after that the procedures are the same as regular PDF creation.

Orbling
  • 20,413
  • 3
  • 53
  • 64
  • Thanks! Nice and easy to overlay text through code as I want to. – filip-fku Nov 28 '10 at 22:38
  • If you just want to fill out a form, do consider the FDF route discussed by moontear, as it is preferable to annotation. If you need to make more substantial changes, then FPDI on TCPDF is necessary. – Orbling Nov 28 '10 at 22:41
4

You probably want to use PDF Forms for what you want to do. To fill these babies you could use the FDF method described here: Using HTML forms to fill in PDF fields with PHP and FDF.
There is actually another nice SO post about PDF form filling here: Filling PDF Forms with PHP.

Community
  • 1
  • 1
Dennis G
  • 21,405
  • 19
  • 96
  • 133
  • +1 Because FDF is the best method for form modification if you do not need to make annotations that forms can not cater for. – Orbling Nov 28 '10 at 22:40
  • +1 This is an elegant approach, but my simple doc only needs a couple of text inserts to warrant going all the way, so for me I think the other method suggested is a bit easier (and quicker). Thanks anyway! – filip-fku Nov 28 '10 at 22:49
3

When you want to use PDF lib to write over a document, positionning is time consuming and boring. Dhek is useful for such task: https://github.com/cchantep/dhek/releases .

JSON mapping over PDF page/coordinates is defined, so that you can write with PDF API you prefer.

cchantep
  • 9,118
  • 3
  • 30
  • 41