Add New Data Parsed from a PDF by PDF.co to a Row on MySQL

In this tutorial, we will show you how to parse a PDF Invoice using the PDF.co Document Parser Web API. Using PHP, we will add the parsed data as a new record in a database table. We divided this tutorial into three (3) sections. Feel free to explore each part using the content links above.

Document Parser Template Creation Guide

We will start this tutorial by creating our template. Kindly go to the Document Parser page in PDF.co and click on the New Template link. Here’s a direct link to the Document Parser Template Editor.

Add Field Based On Text Search Object
First, click on the Load Test PDF or Image button to open your document in the editor.

Then, click on the +Add Object button and select Add FIELD based on TEXT SEARCH.

Search Invoice Number Using Macros
Let’s get the Invoice number. In the Expression field, enter Invoice number:{{Spaces}}({{Digits}}). Then, check the Regex box.

Do the same for the Issue Date and Amount Due. To save you time, you can download the template here.

 

PHP Sample Code Walkthrough

Let’s explore the PHP sample code and review the parts to add your server and database information. To follow along, here’s the link to the PHP code.

Database Information

In the InsertToDb function, you can add your $servername, $username, $password, and $dbname values.

Database Table Information

From lines 123 to 128, you can add your database table and fields information. If you would like to use our default values, you’ll need a database table called sample_table and fields inv_no, inv_date, and inv_total.

 

Parse PDF and Fill Database Demo

We are now ready to run our program. Kindly download the Google.pdf, Google Invoice.json, program.html, and program.php here. Be sure to add your server and database information in the program.php if you’re using different names. Then, move the program.html and program.php files to your /www directory.

In this demonstration, we will only parse the Invoice Number, Invoice Date, and Amount Due from the Google invoice.

Sample PDF Invoice
Sample PDF Invoice

 

Kindly open the program.html. Then, enter your API Key and load the PDF Invoice and JSON template. The Document Parser supports both JSON and YML template formats. Then, click the Proceed button.

Program HTML Interface
Program.html Interface

The program.php page returns the parsing result and confirmation that the parsed data has been added to the database.

PDF.co Document Parser Output

Our sample_table will now have a new record containing the parsed PDF Invoice data.

New Recorded Added In Database
New Record Added In Database

You can watch it in action below.

Parse PDF And Save To Database Demo
Parse PDF and Save to Database Demo

In this tutorial, you learned how to create a Document Parser template. You reviewed the PHP sample code and added your information. You successfully parsed a PDF Invoice and filled the data into your database.