89

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?

On the front-end

mmh4all
  • 1,612
  • 4
  • 6
  • 21
kaybenleroll
  • 16,794
  • 16
  • 54
  • 66
  • I've only used FPDF and think it's awesome. Like really awesome. – SQLMason Oct 17 '11 at 17:02
  • I was trying to find a quick solution to this as well. I wanted the same pdf for each of my product pages but with the product number and product name replaced on each pdf. I found that using mail merge (Word or OpenOffice) is actually the easiest way to do so. Then I exported all the pdfs and uploaded them. Hope this helps somebody. – NotJay Sep 02 '15 at 19:09
  • Why was it considered not focused enough? – Gherman May 20 '21 at 13:13

9 Answers9

72

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.

grom
  • 15,842
  • 19
  • 64
  • 67
  • 1
    This is great! I didn't realise the Zend Framework was free, I was confused by Zend Studio which is proprietary. – Liam Oct 07 '08 at 10:03
  • 8
    Just a word of caution for anyone trying to use this: It only works with PDFs created in Acrobat version 4 and before. After version 4, Adobe started encoding files making it harder to perform edits on PDFs (or import into other PDFs). – Darryl Hein Jun 09 '09 at 01:26
  • 4
    PDF 1.4 Support [has since been added](http://framework.zend.com/manual/en/zend.pdf.introduction.html#fn1). – Raphael Schweikert Apr 14 '11 at 09:49
  • 2
    Does this now work without Zend? – William Entriken Apr 11 '13 at 17:24
  • 1
    Any solution if i wanted to replace content like [placeholder string]? – Starx Aug 26 '13 at 09:28
  • How to edit a PLACEHOLDER text ? – Magico Feb 10 '15 at 16:44
  • For example the primitive for drawing hello world is: [(Hello ) -40 (W) -30 (or) 15 (ld!)] TJ . So editing placeholder text is not easy. – grom Mar 19 '15 at 09:05
  • Hi, could this solution be used to edit signed PDF documents without affecting the signatures of the document? because FPDI for example creates a new document by importing only the pages of the PDF but not the signatures of the document – Aramis Rodríguez Blanco Nov 16 '19 at 14:11
52

There is a free and easy to use PDF class to create PDF documents. It's called FPDF. In combination with FPDI (http://www.setasign.de/products/pdf-php-solutions/fpdi) it is even possible to edit PDF documents. The following code shows how to use FPDF and FPDI to fill an existing gift coupon with the user data.

require_once('fpdf.php'); 
require_once('fpdi.php'); 
$pdf = new FPDI();

$pdf->AddPage(); 

$pdf->setSourceFile('gift_coupon.pdf'); 
// import page 1 
$tplIdx = $this->pdf->importPage(1); 
//use the imported page and place it at point 0,0; calculate width and height
//automaticallay and ajust the page size to the size of the imported page 
$this->pdf->useTemplate($tplIdx, 0, 0, 0, 0, true); 

// now write some text above the imported page 
$this->pdf->SetFont('Arial', '', '13'); 
$this->pdf->SetTextColor(0,0,0);
//set position in pdf document
$this->pdf->SetXY(20, 20);
//first parameter defines the line height
$this->pdf->Write(0, 'gift code');
//force the browser to download the output
$this->pdf->Output('gift_coupon_generated.pdf', 'D');
Alen Šimunic
  • 555
  • 1
  • 7
  • 19
metatron
  • 521
  • 4
  • 2
  • 2
    Excellent library - was exactly what I was after, thanks for the recommendation – james-geldart Jan 26 '12 at 15:53
  • 1
    This is good but in some pdf it gives this error `"FPDF error: This document (testcopy.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI."` Any solution for it? –  Aug 29 '12 at 05:19
  • You need to convert your pdf to an older version (pdf 1.4) or use fpdi commercial edition. *We offer a parser replacement as a separate commercial addon, which enables FPDI to handle documents that uses these compression features.* [source](https://www.setasign.com/support/faq/fpdi/error-document-compression-technique-not-supported/#question-89) – SwissFr Mar 01 '18 at 09:10
  • Is it possible to setSourceFile from another webpage? Ex: example.com/file.pdf? – Alex Jun 30 '20 at 11:23
20

If you need really simple PDFs, then Zend or FPDF is fine. However I find them difficult and frustrating to work with. Also, because of the way the API works, there's no good way to separate content from presentation from business logic.

For that reason, I use dompdf, which automatically converts HTML and CSS to PDF documents. You can lay out a template just as you would for an HTML page and use standard HTML syntax. You can even include an external CSS file. The library isn't perfect and very complex markup or css sometimes gets mangled, but I haven't found anything else that works as well.

Fabien Ménager
  • 140,109
  • 3
  • 41
  • 60
AdamTheHutt
  • 8,287
  • 8
  • 33
  • 33
  • 10
    -1 Since it doesn't answer the question. The poster wants to edit existing PDFs not creating them from scratch. – Reimund Jan 26 '12 at 11:24
  • 13
    I came to this page because I was looking to edit a PDF but this answer seems more useful for me because I can see why may be easier to build up from scratch in html rather than edit an existing PDF. – Justin Vincent Mar 29 '12 at 21:44
3

Don't know if this is an option, but it would work very similar to Zend's pdf library, but you don't need to load a bunch of extra code (the zend framework). It just extends FPDF.

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

Here you can basically do the same thing. Load the PDF, write over top of it, and then save to a new PDF. In FPDI you basically insert the PDF as an image so you can put whatever you want over it.

But again, this uses FPDF, so if you don't want to use that, then it won't work.

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
3

Zend Framework can load and edit existing PDF files. I think it supports revisions too.

I use it to create docs in a project, and it works great. Never edited one though.

Check out the doc here

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Juan
  • 3,433
  • 29
  • 32
2

The PDF/pdflib extension documentation in PHP is sparse (something that has been noted in bugs.php.net) - I reccommend you use the Zend library.

Ross
  • 46,186
  • 39
  • 120
  • 173
1

Tcpdf is also a good liabrary for generating pdf in php http://www.tcpdf.org/

Mufaddal
  • 5,398
  • 7
  • 42
  • 57
  • For everyone searching how to add Header and Footer easily, TCPDF has a very simple and effective example: https://tcpdf.org/examples/example_003/ – Alen Šimunic Jul 01 '18 at 08:43
  • 4
    tcPDF is for generating new PDF. The question is about modifying preexisting PDFs. – Gherman Jul 15 '21 at 19:16
-1

We use pdflib to create PDF files from our rails apps. It has bindings for PHP, and a ton of other languages.

We use the commmercial version, but they also have a free/open source version which has some limitations.

Unfortunately, this only allows creation of PDF's.

If you want to open and 'edit' existing files, pdflib do provide a product which does this this, but costs a LOT

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
-2
<?php

//getting new instance
$pdfFile = new_pdf();

PDF_open_file($pdfFile, " ");

//document info
pdf_set_info($pdfFile, "Auther", "Ahmed Elbshry");
pdf_set_info($pdfFile, "Creator", "Ahmed Elbshry");
pdf_set_info($pdfFile, "Title", "PDFlib");
pdf_set_info($pdfFile, "Subject", "Using PDFlib");

//starting our page and define the width and highet of the document
pdf_begin_page($pdfFile, 595, 842);

//check if Arial font is found, or exit
if($font = PDF_findfont($pdfFile, "Arial", "winansi", 1)) {
    PDF_setfont($pdfFile, $font, 12);
} else {
    echo ("Font Not Found!");
    PDF_end_page($pdfFile);
    PDF_close($pdfFile);
    PDF_delete($pdfFile);
    exit();
}

//start writing from the point 50,780
PDF_show_xy($pdfFile, "This Text In Arial Font", 50, 780);
PDF_end_page($pdfFile);
PDF_close($pdfFile);

//store the pdf document in $pdf
$pdf = PDF_get_buffer($pdfFile);
//get  the len to tell the browser about it
$pdflen = strlen($pdfFile);

//telling the browser about the pdf document
header("Content-type: application/pdf");
header("Content-length: $pdflen");
header("Content-Disposition: inline; filename=phpMade.pdf");
//output the document
print($pdf);
//delete the object
PDF_delete($pdfFile);
?>
kaybenleroll
  • 16,794
  • 16
  • 54
  • 66
Nitin
  • 29
  • 1