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 use this sample PDF form and use coordinates to add text.
Below is the following step-by-step guide to editing PDF in PHP.
Step 1: Start Apache Server
- First, let’s start the Apache server so we can run our program in the local browser.
Step 2: Source Code
- Next, add the PHP sample code in the Visual Studio Code editor. You can also use your favorite editor in PHP. This source code is available at PDF.co API Docs.
Step 3: Setup PHP Code
Let’s set up the PHP code
- In line 21, enter your preferred, output file name.
- In line 22, input the direct URL of your source file.
- For the AnnotationString param, 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.
- For the ImageString param, we will also add the coordinates, page, and URL of the image in this format
- For the FieldString param, 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 Program
Now, let’s run our 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 successfully edited the PDF form by adding text and an image using coordinates.
In this article, you learned how to edit PDF in PHP using PDF.co Web API.