Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace part of PDF with PHP

Tags:

php

pdf

I'm looking at the options for building an invoice and ordervoucher generator in PHP. I've created several PDF's for other occasions before with FPDF and TCPDF and both work fine but are sooooo verbose that i'm getting sick of it.

I get a feeling that generating PDF's in any programming language is super verbose.

Is there any way at all to generate a PDF-blueprint with Adobe Acrobat and use that as a template with variable fields/area's? That way i could for example allow the designers overhere create me a letterhead and overall invoice theme and i would only write the content of the variable parts (order lines, totals, due date, buyer details) into the pdf.

To put it simple, a barebones templating system that reads a pdf and outputs it again after variable substitution.

like image 603
ChrisR Avatar asked Mar 14 '11 10:03

ChrisR


2 Answers

You can create a PDF with formfields and replace the content of those fields in the source. You have to replace the exact number of characters though, otherwise the PDF might get corrupted. In your case, where you want to add whole tables, that might not be feasible.

We did something very similar but used a different approach. Using http://www.setasign.de/products/pdf-php-solutions/fpdi/ we loaded the PDF, and put the content we wanted on top (FPDI extends TCPDF or FPDF).

like image 183
konsolenfreddy Avatar answered Sep 20 '22 02:09

konsolenfreddy


You could use the Zend PDF module which is free: http://framework.zend.com/manual/en/zend.pdf.html

See this question for editing existing files or creating placeholders to swap out using PHP: PDF Editing in PHP?

You can use FPDF for creating new PDF files although it has it's flaws (but it very quick to get something created: http://www.fpdf.org/

like image 34
Andrew Avatar answered Sep 22 '22 02:09

Andrew