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

Extract Text from Scanned PDF in PHP using PDF.co Web API

12 Minutes Read

This tutorial uploads a scanned PDF through a local PHP form, extracts its text using OCR, and provides a TXT file to download. PHP runs the upload and conversion requests; PDF.co performs the text extraction.

Step 1: Prepare a Local PHP Server

Use a local PHP environment with the cURL extension enabled, such as XAMPP or WampServer.

Start Apache and create a project folder inside its document root, for example:

htdocs/pdf-to-text

Keep this demonstration on your local machine. The sample form accepts your API key and is not intended to be published as a public upload service.

Step 2: Save the Sample Files

Save these two files from PDF.co’s official sample repository together in your project folder:

Keep both filenames unchanged, including their extensions. The form submits to pdf-to-text.php in the same folder.

Get your API key from the PDF.co dashboard. You will enter it in the local form when testing.

Step 3: Understand the Upload and OCR Request

The PHP handler performs three operations:

  1. Requests a presigned upload URL from /v1/file/upload/get-presigned-url.
  2. Uploads the selected PDF’s bytes to that destination using PUT with Content-Type: application/octet-stream.
  3. Sends the uploaded file’s URL to POST /v1/pdf/convert/to/text.

The upload response contains two different URLs:

  • presignedUrl: The destination used to upload the file.
  • url: The uploaded file’s address, passed to the conversion endpoint.

The PDF to Text endpoint supports OCR for scanned documents. Its page selection is zero-based:

  • 0: First page.
  • 0-2: First three pages.
  • Empty value: All pages.

For another OCR language, add the documented lang setting to the handler’s $parameters array before json_encode(). Refer to the endpoint documentation for supported language codes.

Step 4: Run the Conversion

Open this address in your browser:

http://localhost/pdf-to-text/sample.html

Then:

  1. Enter your PDF.co API key.
  2. Select a small scanned PDF.
  3. Enter the required page range. Clear any preset 0 if you want all pages.
  4. Select Proceed.

This sample uses synchronous processing. For longer conversions, adapt the handler to use async: true and Background Job Check, waiting for success before retrieving the result. Enabling async alone does not add that waiting logic.

Step 5: Check the Conversion Result

After a successful conversion, the handler displays Conversion Result with a link to the extracted text.

An error message is not a completed conversion:

  • A cURL error indicates a transport problem.
  • An unsuccessful HTTP status indicates a request failure.
  • A JSON response containing error: true indicates an API-reported failure.

If no result link appears, review the displayed response and PHP server log. Check whether the upload completed before investigating the OCR request.

Step 6: Save and Review the Text

Open the successful result link and save the text as extracted.txt before the temporary URL expires.

Save the extracted text itself—not the original PDF or the JSON response containing the download link.

Compare the saved text with the scanned document, checking names, numbers, punctuation, and reading order. OCR accuracy depends on scan quality, and TXT output does not preserve the PDF’s complete visual design.

Related Tutorials

See Related Tutorials