New: PDF to E-Invoice for Factur-X/ZUGFeRD, and PDF to PDF/A for archive-ready PDF/A-3b.

Webpage to PDF in PHP using PDF.co Web API

In this tutorial, we will guide you through the process of converting a webpage to a PDF using the PDF.co Web API with PHP. As an example, we will use the Wikipedia main page to demonstrate the conversion into PDF format.

Wikipedia Main Page

Step 1: Copy and Review the Source Code

  • Copy the Sample Code: Open the PHP webpage-to-PDF sample and paste it into your editor. It should call /v1/pdf/convert/from/url. Configure the source webpage URL and output PDF filename for your conversion.
  • Add Your API Key: Log in to your PDF.co account and retrieve your API Key from your dashboard. Replace the placeholder in the sample code with your actual API key.
  • Input the Web Page URL: Input the web page URL you wish to convert into PDF format.
  • Choose the execution mode: If using asynchronous processing, retain the returned job ID and check its status. Download or open the output only after success. Stop on failed or aborted, and set a polling deadline and network request timeouts. Asynchronous processing does not eliminate all timeout risks.
Copy and Review the Source Code

Step 2: Save the PHP and HTML File in the Directory

Once you've edited the code, save the PHP and HTML file in the appropriate directory for local hosting.

  • For XAMPP Users: Save the file in the /htdocs directory.
  • For WampServer Users: Save the file in the /www directory.

Saving the file in these locations ensures that it’s accessible from your local server.

Save the PHP and HTML File in the Directory

Step 3: Start the Apache Module

To execute your PHP script, ensure that your Apache server is running.

  • Open the XAMPP or WampServer control panel.
  • Click Start next to the Apache module to activate the server.
Start the Apache Module

Step 4: Run the PHP Program Locally

Now, let's run the program on your local machine:

  • Open your browser and type localhost/your-folder-name/your-file.php in the address bar.
    • Example: localhost/URL to PDF/web-page-to-pdf-async.php
  • Once the program runs, it will return an output URL.
    • Click on the output link to download the PDF file.
Run the PHP Program Locally

Step 5: Access the Generated PDF Result

Open the generated PDF to view the result.

PDF Result
PDF Result

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.

Video Guide

Related Tutorials

See Related Tutorials