- How to Edit PDF in PHP using PDF.co Web API
- Step 1: Start Apache Server
- Step 2: Source Code
- Step 3: Setup PHP Code
- Step 4: Save Files in the Folder
- Step 5: Run the Program
- Step 6: Output
- Step 7: PDF.co Add Text Image Demo
- Related Posts:
- Add Watermark to PDF in Python using PDF.co Web API
- How to Delete Pages from PDF using PDF.co Web API
- Glorified Geek
- «Play is the highest form of research» – Albert Einstein
- Modifying PDF files with PHP
How to Edit PDF in PHP using PDF.co Web API
PDF documents have become a standard format for sharing and preserving digital content, and having the ability to manipulate them programmatically opens up a world of possibilities. PDF editing in PHP using PDF.co Web API is an exciting and powerful way to manipulate PDF documents programmatically. PDF has become a widely used format for sharing and preserving digital content due to its platform-independent nature and consistent layout across devices.
PDF.co is an API and automation platform for PDF, Barcodes, Data Extraction, and Data Transformations. It also presents online tools for conducting fundamental PDF-related functionalities such as splitting/merging PDF, document parsing, filling PDF forms, searching/replacing text, PDF data extraction to various formats, barcode reader, etc.
In this article, we will show you how to edit PDFs in PHP using PDF.co Web API. The code will be written in PHP and PDF.co will be used to perform the actions.
We will utilize a sample PDF form as our starting point and use coordinate-based positioning to add text dynamically.
Editing PDF documents using PHP can be a complex process, but with the right tools and techniques, it can be done efficiently and effectively. Below, you will find a comprehensive step-by-step guide that outlines the process of editing PDF documents using PHP.
Step 1: Start Apache Server
- To begin, let’s start the Apache server so that we can run our program in the local browser.
Step 2: Source Code
- Next, let’s add the PHP sample code to your preferred editor, such as Visual Studio Code or any other PHP-compatible editor. This source code is available in PDF.co API Docs.
Step 3: Setup PHP Code
- In line 21, enter your preferred, output file name.
- In line 22, input the direct URL of your source file.
- In line 23, add the text coordinates together with the page range and the text you want to add to your PDF document in this format x;y;pages;text;fontsize;fontname;fontcolor;link;transparent;width;height;alignment . You can get the PDF coordinates for the text and image in this link.
- In line 24, we will also add the coordinates, page, and URL of the image in this format x;y;pages;urltoimageOrPdf;linkToOpen;width;height.
- In line 25, set the values for fillable pdf fields. To fill fields in PDF form, use the following format page;fieldName;value. . You can get the PDF info field name at this link.
- In line 29, add your PDF.co API Key. You can get the API Key in your PDF.co dashboard.
Step 4: Save Files in the Folder
- Then, save the PHP sample code in your program folder.
- We highly recommend saving the files in a folder inside the \www or the \htdocs directory.
Step 5: Run the Program
- In the browser address bar, type in localhost/folder-name/sample.php . The /folder-name/ is a folder in the /htdocs directory where you stored the files if you are using XAMPP Server.
- Then, copy the temporary URL and paste it into your browser to view the output.
Step 6: Output
- We edited the PDF form by adding text and an image using coordinates.
Step 7: PDF.co Add Text Image Demo
- Here is the Add Text and Image to PDF in action.
This comprehensive article explores the exciting world of PDF editing in PHP using PDF.co Web API. PDF documents have become an extensive format for sharing and preserving digital content, and being able to manipulate them programmatically opens up a wide range of possibilities.
Related Posts:
Add Watermark to PDF in Python using PDF.co Web API
In general, a watermark can be described as a pattern or image, which appears superimposed on a paper when viewed Also, [. ]
How to Delete Pages from PDF using PDF.co Web API
The tutorial and the sample source code explain how to delete any page from a PDF document using JavaScript This [. ]
Glorified Geek
«Play is the highest form of research» – Albert Einstein
Modifying PDF files with PHP
Last week, a friend of mine asked me to help him with a programming problem that he had been wrestling with for some time. The problem sounds simple:
And this had to be done with PHP.
Although there are several libraries available in PHP for dealing with PDF files, none seem to have capabilities to modify the contents of an existing PDF file. Their manuals/tutorials are full of examples on how to create PDF on the fly. After spending few fruitless hours trying to get the much recommended PDFLib installed in my Mac and have it work with MAMP, I painfully realized this library is for commercial use only. The free version leaves a horrible watermark of their site address on the generated PDF documents.
My search for a solution took me to FPDF, an open-source library for PDF file generation in PHP. In their FAQ section, I found the link to an extension of the library, named FPDI. This one was seemingly capable of ‘manipulating’ PDF files in an ad hoc fashion. It extracts the contents of each page in the file, uses it as a template, lets you put texts/shapes on the template and then outputs the modified file. Excited, I got into coding and after an hour of labor, finally succeeded to achieve my goal! Thank God for creating open source!
Enough talk, now lets get our hand dirty!
First we need to have following libraries downloaded and unzipped. They are just packages of PHP scripts that you just require/include in your own script. No need to deal with .dll/.so extensions.
Keep them in the same directory of your script, or in the include path. The following code snippet gives a basic idea of how to get started with it:
//Set the source PDF file
$pagecount = $pdf->setSourceFile(«my_existing_pdf.pdf»);
//Import the first page of the file
$tpl = $pdf->importPage($i);
//Use this page as template
$pdf->useTemplate($tpl);
#Print Hello World at the bottom of the page
//Go to 1.5 cm from bottom
$pdf->SetY(-15);
//Select Arial italic 8
$pdf->SetFont(‘Arial’,’I’,8);
//Print centered cell with a text in it
$pdf->Cell(0, 10, «Hello World», 0, 0, ‘C’);
The above code takes a PDF file “my_existing_pdf.pdf”, and creates a copy of it “my_modified_pdf.pdf” with “Hello World” printed at the centre bottom of the first page.
That’s it! To achieve my goal, which I outlined at the start of this post, I extended the FPDI class, and overrode the Footer() method to print a customized footer in each page.
I only wish that the PHP online manual did NOT have an entire section dedicated to PDFLib, a non-free and commercial library, and rather point to free ones such FPDF or TCPDF. It could have saved me hours.