How to Split PDF by Text Asynchronously in PHP using PDF.co Web API

Nov 26, 2024·3 Minutes Read

In this tutorial, we will guide you through the process of splitting a PDF by text asynchronously in PHP using the PDF.co Web API. This method allows you to split large PDF files based on specific text or keywords while taking advantage of asynchronous processing to avoid timeout errors. We will demonstrate this using a sample PDF file.

Sample PDF Document

Step 1: Source Code

  • Copy the Sample Code: First, copy the PHP sample code for the PDF.co Split PDF by Text API. Paste the code into your preferred editor (e.g., VS Code, Sublime Text, or PHPStorm).If you prefer to upload your file directly from your local environment, you can use the following sample code: PHP and HTML Code.
  • 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 PDF URL: If your PDF file is available online, use its direct URL in the code. If you are working with a local PDF file, upload it to the PDF.co file storage and use the returned file token as your PDF URL.
  • Enter Split Search Text: Identify the text or keyword you want to use as the splitting point in the PDF (e.g., "Invoice Number" or "Order ID"). This text will determine where the PDF is divided into separate documents.
  • Use Asynchronous Processing: Asynchronous jobs are useful for large PDFs, as they allow you to track the job’s progress without worrying about timeouts. This method ensures that your request runs in the background. For more detailed information on asynchronous jobs, refer to the documentation.
PHP Source Code

Step 2: Save the PHP File in the Correct Directory

After editing the code, save the file in the appropriate folder:

  • For XAMPP users, save it in the /htdocs directory.
  • For WampServer users, save it in the /www directory.

This ensures that the PHP file is accessible from your localhost.

Save PHP File

Step 3: Start the Apache Module

To run your PHP program, make sure the Apache server is up and running.

  • Open the XAMPP or WampServer control panel.
  • Click Start on the Apache module.
Start the Apache Module

Step 4: Run the PHP Program

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/PDF Split/split-pdf-async.php
  • Once the program runs, you will receive an output URL.
    • Click on the output link to view the results, which will contain the split PDF files.
Run the PHP Program

In this tutorial, you learned how to split PDF by text in PHP using PDF.co Web API. You also learned how to run the program using an Apache Server.

Related Tutorials

See Related Tutorials