How to convert PDF to CSV from uploaded file for PDF to CSV API in PHP with PDF.co Web API

Step By Step Tutorial: how to convert PDF to CSV from uploaded file for PDF to CSV API in PHP

Here you may find thousands of pre-made source code pieces for easy implementation in your own programming projects. PDF.co Web API helps with PDF to CSV API in PHP. PDF.co Web API is the flexible Web API that includes a full set of functions from e-signature requests to data extraction, OCR, image recognition, pdf splitting, and PDF merging. Can also generate barcodes and read barcodes from images, scans and pdf.

The SDK samples like this one below explain how to quickly make your application do PDF to CSV API in PHP with the help of PDF.co Web API. For the implementation of this functionality, please copy and paste the code below into your app using the code editor. Then compile and run your app. Further enhancement of the code will make it more vigorous.

ByteScout free trial version is available for FREE download from our website. Programming tutorials along with source code samples are included.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

Let’s review source code and it’s output first, and then we’ll analyze important code snippets briefly. Full source code used in this article can also be found at our GitHub repository at this location.

Source Code

pdf-to-csv.php

      
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PDF To CSV Extraction Results</title> </head> <body> <?php // Get submitted form data $apiKey = $_POST["apiKey"]; // The authentication key (API Key). Get your own by registering at https://app.pdf.co/documentation/api $pages = $_POST["pages"]; // 1. RETRIEVE THE PRESIGNED URL TO UPLOAD THE FILE. // * If you already have the direct PDF file link, go to the step 3. // Create URL $url = "https://api.pdf.co/v1/file/upload/get-presigned-url" . "?name=" . $_FILES["file"]["name"] . "&contenttype=application/octet-stream"; // Create request $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPHEADER, array("x-api-key: " . $apiKey)); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // Execute request $result = curl_exec($curl); if (curl_errno($curl) == 0) { $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($status_code == 200) { $json = json_decode($result, true); // Get URL to use for the file upload $uploadFileUrl = $json["presignedUrl"]; // Get URL of uploaded file to use with later API calls $uploadedFileUrl = $json["url"]; // 2. UPLOAD THE FILE TO CLOUD. $localFile = $_FILES["file"]["tmp_name"]; $fileHandle = fopen($localFile, "r"); curl_setopt($curl, CURLOPT_URL, $uploadFileUrl); curl_setopt($curl, CURLOPT_HTTPHEADER, array("content-type: application/octet-stream")); curl_setopt($curl, CURLOPT_PUT, true); curl_setopt($curl, CURLOPT_INFILE, $fileHandle); curl_setopt($curl, CURLOPT_INFILESIZE, filesize($localFile)); // Execute request curl_exec($curl); fclose($fileHandle); if (curl_errno($curl) == 0) { $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($status_code == 200) { // 3. CONVERT UPLOADED PDF FILE TO CSV ExtractCSV($apiKey, $uploadedFileUrl, $pages); } else { // Display request error echo "<p>Status code: " . $status_code . "</p>"; echo "<p>" . $result . "</p>"; } } else { // Display CURL error echo "Error: " . curl_error($curl); } } else { // Display service reported error echo "<p>Status code: " . $status_code . "</p>"; echo "<p>" . $result . "</p>"; } curl_close($curl); } else { // Display CURL error echo "Error: " . curl_error($curl); } function ExtractCSV($apiKey, $uploadedFileUrl, $pages) { // Create URL $url = "https://api.pdf.co/v1/pdf/convert/to/csv"; // Prepare requests params $parameters = array(); $parameters["url"] = $uploadedFileUrl; $parameters["pages"] = $pages; // Create Json payload $data = json_encode($parameters); // Create request $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPHEADER, array("x-api-key: " . $apiKey, "Content-type: application/json")); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Execute request $result = curl_exec($curl); if (curl_errno($curl) == 0) { $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($status_code == 200) { $json = json_decode($result, true); if ($json["error"] == false) { $resultFileUrl = $json["url"]; // Display link to the file with conversion results echo "<div><h2>Conversion Result:</h2><a href='" . $resultFileUrl . "' target='_blank'>" . $resultFileUrl . "</a></div>"; } else { // Display service reported error echo "<p>Error: " . $json["message"] . "</p>"; } } else { // Display request error echo "<p>Status code: " . $status_code . "</p>"; echo "<p>" . $result . "</p>"; } } else { // Display CURL error echo "Error: " . curl_error($curl); } // Cleanup curl_close($curl); } ?> </body> </html>

Output

Now that we’ve already seen the program source code and its output, let’s review code a bit. We can logically divide the source code into two parts. The first part would be uploading input PDF and getting public URL, and the second part is processing PDF to CSV conversion using that URL.

PDF.co API endpoint /v1/file/upload/get-presigned-url is being used to upload file to PDF.co and get a public URL. This output URL is actually a temporary URL which is valid for a few minutes. We’re passing only input file name with this API request and its returning two URLs in response. One URL is pre-signed URL ($json[“presignedUrl”]), and the other one is a public URL for the input file ($json[“url”]). After this output, we’re proceeding with uploading the actual file to this pre-signed URL with PUT request. With the uploading completed, we’re now ready to utilize the uploaded file URL for CSV conversion.

PDF.co endpoint /v1/pdf/convert/to/csv is being used to convert from PDF to CSV. The required input parameters are very minimum such as URL of input and page numbers which need to be converted. The output of this request primarily consists of URL of the converted CSV document.

Please refer to PDF.co documentations for more information regarding other available input parameters. This is a very simplified version of PDF to CSV convertion, we can always construct an advanced request as per our requirement.

To get more out of this article, please try to implement this sample in your machine. Thank you for reading!

VIDEO

ON-PREMISE OFFLINE SDK

Get 60 Day Free Trial

See also:

ON-DEMAND REST WEB API

Get Your API Key

See also:

Related Samples: