Extracting Invoices using Invoice Splitter Tutorial (Node.js)
Overview
When to use this?
In some instances, a single invoice file can contain more than one invoice. Whether they be from the same provider or a different one, it might be a hassle to check them to be able to enqueue them individually. This is where the Invoice Splitter API comes in.
The Invoice Splitter API takes in a multipage invoice, and gives you the index of each page of the individual invoices.
From there, you can cut your file into sub-invoices based off of these indices.
Initial Setup
The goal of this tutorial is to use the Invoice Splitter API to isolate invoices and then parse them on the Invoice OCR.
To do this, we'll be using the following dummy file:
https://github.com/mindee/client-lib-test-data/blob/249c253aa93cd9dd10e235dc5ec0cacb2536f7ed/products/invoice_splitter/default_sample.pdf
The file is a simple aggregate of same invoices, made up for this tutorial. For best results, we encourage you try your own!
Before uploading, be sure that you have taken the following into account:
Invoices are clear, unstained and properly unfolded
As little extra useless pages as possible (e.g. terms & condition)
Pages from any single invoice are all oriented in the same direction
The Invoice Splitter API is quite robust, but it will not distinguish between other types of documents correctly if they are mixed inside of the PDF file.
Subscribing to the Invoice Splitter API
For this tutorial, you'll need to be in possession of a valid and up-to-date API key (and a valid subscription to the Invoice Splitter API as well as the Invoice OCR).
If you aren't sure whether your subscription is enabled or not, go on the API page of the main interface, click on Utilities:

Then click on the Invoice splitter button. That's it, your subscription to the API should be enabled.
To do the same for the Invoice OCR, simply head to the Document Catalog:

Then, like in the previous step, click on our product of interest (Invoice).
Calling the API
Time to get coding!
For this tutorial, we'll be using the official Mindee client library for Node.js.
Other programming languages are supported, check the list.
Let's create a directory to store our project and install the Mindee client:
To get started with the code, we'll head on over to the Documentation page by using the link on the left of the interface.
From there we'll click on your language (NODEJS), then Select an API key, and finally use the copy code button:

Using your IDE, open the demo_invoice_splitter file we created, and paste this code. We'll use it as a base.
Make sure the API key is correctly filled in, if not, simply grab one from the Mindee interface and put it into demo_invoice_splitter.js
Find the line:
And put in a real file path on your drive. You should test this file in the live interface before checking.
Now run the code:
The result should look something like this:
Image extraction code
The method to call to extract the images is called in the following way:
We'll add the method call to the response handling part of the script. You should have something like this:
This is not very pretty, though.
We can make this a bit more legible and usable by switching to something like this.
Parsing the Extracted Files
Now one last piece is missing: what to do with our invoices? Well, parse them, of course!
The code for this part is mostly up to you, but for this tutorial we'll use a simple loop to parse the documents and then display the results:
This should print something like this:
Despite showing a filename, the results shouldn't needlessly create temporary files unless you specify it to, as everything is managed through file buffers.
A working implementation of this code can be found in the example/invoiceSplitterTutorial.js file on the Node.js repo.
Custom Implementation
If you are using a non-supported language, or simply want to implement things your own way, here's a gist of how things work under the hood...
But first, if you are not familiar with other products yet, know that a response from the API will contain a document field, which in turns contains the following attributes of interest: inference > prediction and inference > pages. pages being a list of objects, all containing their own prediction attributes. Think of the inference's prediction as an aggregate of these.
Why are these predictions relevant? Because they contain our invoices' page groups. Which in turn contains a list of pages belonging to a single invoice, which is simply a number from 0 to n, where n is the last page index of your PDF.
The next step is pretty straightforward: use the indexes of each page group to cut out a page from the original file, and then create a new PDF file containing those pages.
Last updated
Was this helpful?

