How to edit pdf file in php

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

How to edit pdf file in php

sensorario

18.8k27 gold badges93 silver badges151 bronze badges

asked Aug 10, 2008 at 21:58

kaybenlerollkaybenleroll

16.6k16 gold badges53 silver badges66 bronze badges

3

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:

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.

answered Aug 11, 2008 at 0:49

8

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');

How to edit pdf file in php

answered Nov 15, 2010 at 12:31

metatronmetatron

5114 silver badges2 bronze badges

4

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.

answered Aug 27, 2008 at 9:59

AdamTheHuttAdamTheHutt

7,9178 gold badges32 silver badges33 bronze badges

2

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.

answered Sep 9, 2008 at 22:46

Darryl HeinDarryl Hein

140k89 gold badges213 silver badges259 bronze badges

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

How to edit pdf file in php

bad_coder

9,38619 gold badges37 silver badges61 bronze badges

answered Aug 10, 2008 at 23:16

JuanJuan

3,43529 silver badges32 bronze badges

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.

answered Aug 21, 2008 at 16:15

RossRoss

45.2k37 gold badges118 silver badges170 bronze badges

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

answered May 29, 2012 at 12:38

How to edit pdf file in php

MufaddalMufaddal

5,3707 gold badges43 silver badges57 bronze badges

2

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

answered Aug 10, 2008 at 22:41

Orion EdwardsOrion Edwards

119k60 gold badges234 silver badges321 bronze badges


kaybenleroll

16.6k16 gold badges53 silver badges66 bronze badges

answered Oct 21, 2009 at 5:49

NitinNitin

191 bronze badge

5

Can we edit PDF in PHP?

PSPDFKit for Web is a JavaScript library for editing PDF documents directly in a browser. It is fully compatible with PHP and offers developers a robust API for programmatic editing, as well as a beautiful UI for performing a host of editing operations in any PHP-based web app.

How can I edit my PDF file?

How to edit PDF files:.
Open a file in Acrobat..
Click on the “Edit PDF” tool in the right pane..
Use Acrobat editing tools: Add new text, edit text, or update fonts using selections from the Format list. ... .
Save your edited PDF: Name your file and click the “Save” button..

How can I edit a PDF file in HTML?

Learn how to convert a PDF to an HTML web page..
Open the file you want to convert in your PDF editor..
Select the Create & Edit button on the right-side toolbar..
Click Export PDF at the top of the window..
Choose HTML Web Page and select your options..

Can PHP read PDF?

Note: PHP is not actually reading the PDF file. It does not recognize File as pdf. It only passes the PDF file to the browser to be read there. If copy the pdf file inside htdocs folder of XAMPP then it does not need to specify the file path.