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'); ?>
Add new text to a PDF. Open your file in the Acrobat PDF Editor. Select Fill & Sign on the right side of the screen. Choose the Add Text tool, which looks like an upper-case “A” next to a lower-case “b.” Click anywhere in the PDF where you'd like to add text and start typing.
Draw on any PDF document. Launch Acrobat and select File > Open to bring up your PDF. From the menu bar on the right, select Comment. Select the marker icon in the Comment toolbar to activate the Draw Free Form tool. Draw on the PDF.
A PDF document can be embeded in a Web page using plain HTML files or HTML generated by PHP or any other server side language.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With