Skip to content
Advertisement

PDF Editing in PHP? [closed]

Does anyone know of a good method for editing PDFs in PHP? Preferably open-source/zero-license cost methods. 🙂

I am thinking along the lines of opening a PDF file, replacing text in the PDF and then writing out the modified version of the PDF?

I have programmatically created PDF files in the past using FPDF, but found it a little unwieldy at times.

Advertisement

Answer

If you are taking a ‘fill in the blank’ approach, you can precisely position text anywhere you want on the page. So it’s relatively easy (if not a bit tedious) to add the missing text to the document. For example with Zend Framework:

<?php
require_once 'Zend/Pdf.php';

$pdf = Zend_Pdf::load('blank.pdf');
$page = $pdf->pages[0];
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$page->setFont($font, 12);
$page->drawText('Hello world!', 72, 720);
$pdf->save('zend.pdf');

If you’re trying to replace inline content, such as a “[placeholder string],” it gets much more complicated. While it’s technically possible to do, you’re likely to mess up the layout of the page.

A PDF document is comprised of a set of primitive drawing operations: line here, image here, text chunk there, etc. It does not contain any information about the layout intent of those primitives.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement