In this tutorial, we will show you how to convert a webpage to PDF in PHP using 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.

  1. Source Code
  2. Start Apache
  3. Run Program
  4. PDF Output
  5. Demo
Wikipedia Page And PDF Output
Wikipedia Page and 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.

Save Code Samples In Folder

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 API Key and the webpage we want to convert to PDF.

Run Program

Step 4 – PDF Output will then return a temporary URL that we can use to view and download the PDF output.

PDF Output

Step 5 – Demo

Here’s a quick webpage to PDF demo from start to finish.

Webpage To PDF Demo
Webpage to PDF Demo

In this tutorial. you learned how to convert a webpage to PDF in PHP using Web API. You also learned how to set up the environment to run the program in your localhost.