Add New Data Parsed from a PDF 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 sections. Feel free to explore each part using the table of contents.
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.
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 from TEXT SEARCH.
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.
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.
In the InsertToDb function, you can add your $servername
, $username
, $password
, and $dbname
values.
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 sample files 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.
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.
The program.php page returns the parsing result and confirmation that the parsed data has been added to the database.
Our sample_table will now have a new record containing the parsed PDF Invoice data.
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.