How to Create PDF from Scratch in PHP using PDF.co Web API
In this article will demonstrate how to create a PDF from scratch in PHP. We will use the PDF.co Web API to do this task.
Contents
Step 1: Start Apache Server
- First, let’s start the Apache server so we can run the program in the local browser.
![Start Apache Server](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2F2niroq9z%2Fproduction%2F56f6d00f58fb619f2681578a39b5afabd03c9779-835x515.png&w=1920&q=75)
Step 2: PHP Source Code
- Next, copy the PHP source code. Save it in a folder inside the /www or /htdocs directory depending on whether you are using XAMPP or WampServer.
![PHP Source Code](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2F2niroq9z%2Fproduction%2F85a76b35ccf4be1b990b1f3d84d8a88a9286322d-831x185.png&w=1920&q=75)
Step 3: Setup Code
Let’s set up the code
- In line 21, type in your desired output PDF file name.
- In line 22, let’s leave the URL empty so we can create a new PDF.
- In line 29, add your API Key. You can get the API Key in your PDF.co dashboard.
For the Annotation String param
- add the X and Y coordinates of your text together with the page range.
- Next, enter the text you want to add to your PDF document.
- And add the font size, font name, and font color of your text.
For Image String param
- We will add the coordinates and page of our image together with the direct URL.
For Field String param
- This is usually used for filling out form fields. Since we’re creating a PDF from scratch, we will just skip this param.
![Setup Code](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2F2niroq9z%2Fproduction%2F3c0f82e4c0e5a683240fcab98a9af7571cd9afb1-999x203.png&w=2048&q=75)
Step 4: Run Program
- Now, let’s run the program, on your browser type
localhost/Name of folder/PHP filename
. - Click on the Output File URL to view the generated PDF.
![Run Program](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2F2niroq9z%2Fproduction%2F758bafd90aea24d7f25f13867fcbc44db77dfa24-663x305.png&w=1920&q=75)
Step 5: Output PDF
- Here’s the generated PDF output from scratch
![Generated PDF from Scratch](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2F2niroq9z%2Fproduction%2Fced0853db351794d98ef954f79c1fece73758473-999x698.png&w=2048&q=75)
In this article, you learned how to create PDFs from scratch in PHP using PDF.co Web API. You can check out how it can be done in C# or JavaScript as well.