DEV Community

medhakamath
medhakamath

Posted on Edited on

How to Extract Structured JSON from PDF Invoices in Python (Without Messy Regex)

Parsing PDF invoices and receipts with fragile regular expressions is a common developer frustration. This guide demonstrates how to use Python to convert PDFs (via local files, public URLs, or raw text) into clean, normalized accounting JSON using the PDF Invoice & Receipt Data Extractor API on RapidAPI.

What We Will Extract

The API normalizes key fields such as vendor details (vendor_name, vendor_email), invoice metadata (invoice_number, invoice_date, currency), and financial amounts (subtotal_amount, tax_amount, total_amount).

Prerequisites

  1. Python 3.7+
  2. The requests library (pip install requests)
  3. A free API key from your PDF Invoice & Receipt Data Extractor API listing (Basic tier includes 50 free requests/month).

Core Extraction Methods

1. Local PDF File (/v1/parse-pdf-file)

Use multipart/form-data to upload a local PDF file:

import requests

url = "https://rapidapi.com"
headers = {
    "x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
    "x-rapidapi-host": "pdf-invoice-receipt-data-extractor.p.rapidapi.com"
}

with open("invoice.pdf", "rb") as pdf_file:
    response = requests.post(
        url,
        headers=headers,
        files={"file": ("invoice.pdf", pdf_file, "application/pdf")}
    )

if response.status_code == 200:
    print(response.json())
Enter fullscreen mode Exit fullscreen mode

2. Public PDF URL (/v1/parse-pdf-url)

Pass a direct HTTPS link to a hosted PDF without downloading it first:

import requests

url = "https://rapidapi.com"
payload = {"url": "https://example.com"}
headers = {
    "content-type": "application/json",
    "x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
    "x-rapidapi-host": "pdf-invoice-receipt-data-extractor.p.rapidapi.com"
}

response = requests.post(url, json=payload, headers=headers)
print(response.json())
Enter fullscreen mode Exit fullscreen mode

3. Raw Invoice Text (/v1/parse-invoice-text)

Send extracted text strings directly from emails or OCR pipelines:

import requests

url = "https://rapidapi.com"
payload = {"text": "ACME Cloud Services Inc.\nInvoice #INV-2026-8841\nTotal Due: \$295.00 USD"}
headers = {
    "content-type": "application/json",
    "x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
    "x-rapidapi-host": "pdf-invoice-receipt-data-extractor.p.rapidapi.com"
}

response = requests.post(url, json=payload, headers=headers)
print(response.json())
Enter fullscreen mode Exit fullscreen mode

Source Code & Examples

You can find the documentation in the GitHub Repository.

Wrapping Up

Test the live endpoints directly on the PDF Invoice & Receipt Data Extractor RapidAPI page and share your feedback in the comments!

Top comments (0)