Webpage to PDF in PHP using PDF.co Web API
Nov 20, 2024·2 Minutes Read
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.
Step 1: Copy and Review the Source Code
- Copy the Sample Code: Start by copying the PHP sample code for the PDF.co PDF to CSV API. Paste it into your preferred editor, such as VS Code, Sublime Text, or PHPStorm.
- 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.
- Enable Asynchronous Processing: Asynchronous mode is ideal for large PDFs, allowing you to monitor the job's progress without risking a timeout. For more details, refer to PDF.co’s Asynchronous Processing Documentation.
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.
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.
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
- Example:
- Once the program runs, it will return an output URL.
- Click on the output link to download the PDF file.
Step 5: Access the Generated PDF Result
Open the generated PDF to view the 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
Convert URL to PDF using PDF.co and Make
Sep 11, 2024·3 Minutes Read
How to Create PDF from URL/HTML with Puppeteer
Sep 2, 2024·7 Minutes Read
Convert Webpage to PDF Online using PDF.co
Sep 2, 2024·10 Minutes Read