Inkspan › Guides › How to extract text from a PDF in Node.js

How to extract text from a PDF in Node.js

The /v1/extract/text endpoint returns the text content of a PDF as JSON, split per page. Each entry carries its page number and the text found on it, which keeps positional context you would lose by concatenating the whole document into one string.

This reads the text layer rather than performing OCR. Born-digital PDFs — anything produced by software, which covers most invoices, statements, and exports — extract cleanly and quickly.

Also in: cURL · Python

Node.js example

import fs from 'node:fs';

const fd = new FormData();
fd.append('file', new Blob([fs.readFileSync('input.pdf')]), 'input.pdf');

const res = await fetch('https://api.inkspan.dev/v1/extract/text', {
  method: 'POST',
  headers: { 'X-API-Key': process.env.INKSPAN_KEY },
  body: fd,
});
if (!res.ok) throw new Error(await res.text());
console.log(await res.json());

Request parameters

NameTypeRequiredDescription
filefileyesThe PDF to read text from.

Response

Returns application/json:

{
  "page_count": 2,
  "pages": [
    {
      "page": 1,
      "text": "Invoice INV-1042\nTotal: $61.00"
    },
    {
      "page": 2,
      "text": "Terms and conditions\u2026"
    }
  ]
}

Things that catch people out

Errors

StatusMeaningFix
401Invalid or missing API keySend your key in the X-API-Key header.
402Monthly quota exceededThe response reports used/quota. Upgrade at inkspan.dev/#pricing.
422Missing or invalid parameterCheck required fields in the table above — the body names the offending field.

Failed requests do not count against your quota; only successful ones are metered.

Frequently asked questions

Does it handle scanned PDFs?

No. Extraction reads the embedded text layer; a scan without one returns empty pages. Rasterise with /v1/pdf/to-images and run OCR or a vision model if you need that.

How do I tell a scanned PDF from a digital one?

Extract and check whether the text is empty. Empty pages with a non-trivial page count almost always mean a scan.

Is layout preserved?

Text comes back per page in the document's internal order. For tabular data use /v1/extract/tables, which reconstructs row and column structure.

Get a free API key →

Related Extract guides: extract tables from a PDF