In this tutorial, we will show you how to convert a webpage to PDF in PHP using PDF.co Web API. To demonstrate, we will convert the Wikipedia main page to PDF. Below is the image of the Wikipedia page and its PDF output.
Step 1 – Source Code
First, let’s copy the HTML code and the PHP code here and save them in a folder inside /www or /htdocs directory.
Step 2 – Start Apache
Next, start the Apache server so we can run the files in our localhost.
Step 3 – Run Program
To run the program, open a browser and type in
localhost/folder_name/sample.html. This will open the HTML file where we can enter our PDF.co API Key and the webpage we want to convert to PDF.
Step 4 – PDF Output
PDF.co will then return a temporary URL that we can use to view and download the PDF output.
Step 5 – Demo
Here’s a quick webpage to PDF demo from start to finish.
In this tutorial. you learned how to convert a webpage to PDF in PHP using PDF.co Web API. You also learned how to set up the environment to run the program in your localhost.