How to Convert PDF to CSV from Uploaded File for PDF to CSV API in PHP with PDF.co Web API
What is PDF.co Web API? It is the Rest API that provides set of data extraction functions, tools for documents manipulation, splitting and merging of PDF files. This PDF to CSV converter in PHP includes built-in OCR, images recognition, can generate and read barcodes from images, scans and PDF.
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 PDFs.
The API 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.
On-demand (REST Web API) version:
Web API (on-demand version)
On-premise offline SDK for Windows:
60 Day Free Trial (on-premise)
Step-By-Step Tutorial: How to Convert PDF to CSV from Uploaded File for PDF to CSV API in PHP
Let’s review the source code and its output first, and then we’ll analyze important code snippets briefly. The full source code used in this article can also be found at this location.
pdf-to-csv.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PDF To CSV Extraction Results</title> </head> <body> <?php // Note: If you have input files large than 200kb we highly recommend to check "async" mode example. // 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 the code a bit. We can logically divide the source code into two parts. The first part would be uploading the input PDF and getting the 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 that is valid for a few minutes. We’re passing only the input file names with this API request and it’s 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 a 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 the URL of input and page numbers that need to be converted. The output of this request primarily consists of the URL of the converted CSV document.
Please refer to PDF.co documentation for more information regarding other available input parameters. This is a very simplified version of PDF to CSV conversion, 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 TUTORIAL
ON-PREMISE OFFLINE SDK
See also:
ON-DEMAND REST WEB API
Get Your API Key
See also:
PDF-co-Web-API-PHP-Convert-PDF-To-CSV-From-Uploaded-File.pdf