Merging Multiple Documents into a Single PDF Asynchronously in PHP Using PDF.co Web API

Nov 12, 2024·5 Minutes Read

In this guide, we’ll walk you through the process of merging various types of documents into a single PDF using PHP and the PDF.co Web API. We’ll also demonstrate how to handle this process asynchronously, which is especially useful for large files or complex operations that might require additional processing time.

Multiple Documents to be Merged
Multiple document types to be merged

Step 1: Copy and Review the Source Code

Begin by copying the provided HTML and PHP source code. Paste this code into your preferred code editor, such as VS Code, Sublime Text, or PHPStorm.

Using asynchronous processing is recommended for larger files. This allows your job to run in the background, making it possible to track progress without dealing with potential timeouts. For more details on asynchronous jobs, refer to PDF.co's documentation.

Step 2: Save the PHP File in the Correct Directory

Once you've edited the code, save the PHP 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.

Server 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 Apache Module

Step 4: Run the PHP Program Locally

Open your browser and enter the following in the address bar to access the file:

  • URL Path: If you saved the file as sample.html in a folder named MergeAnyDocuments, enter localhost/MergeAnyDocuments/sample.html.
  • 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.
  • Select the Files for Merging: Specify the files you wish to merge into a single PDF by selecting or uploading them as prompted by the program.
  • Send the Merge Request: Click the Proceed button to initiate the merge process. This request will send your files to PDF.co’s Web API for merging.
Run the PHP Program Locally

Step 5: Access the Merged PDF Result

Once the process completes, a link to the output file will be provided. Click on this link to download or view the merged PDF document.

Access the Merged PDF Result

Step 6: Verify the Merged PDF

Open the merged PDF to confirm that all selected documents were successfully combined into a single file. Review the document to ensure that all content is as expected.

Verify the Merged PDF

In this tutorial, you learned how to merge PDFs, images, and documents in PHP using PDF.co Web API. You also learned how easy it is to set up the code samples to get your program running right away.

Video Guide

Related Tutorials

See Related Tutorials