← PDF to text
Extract text from a PDF in Node.js
Extract the text from a PDF, keeping the layout or as plain reading order, for all pages or a range. Here is the whole call in Node.js, with the output it produced when we ran it.
Before you start
- An API key. Create a free account; it comes with free tokens every month. Put the key in an environment variable so it stays out of your code:
export BLOODWEB_KEY=your_key - Node.js 18 or later. Nothing to install:
fetch,FormDataandBlobare built in. Save the file with the.mjsending soawaitworks at the top level. - The input file in the same folder:
report.pdf. Use your own; that is the name the code expects.
The code
Save it as pdf-to-text.mjs and run node pdf-to-text.mjs.
// Node.js 18 or later: fetch, FormData and Blob are built in.
import { readFile, writeFile } from "node:fs/promises";
const form = new FormData();
form.append("file", new Blob([await readFile("report.pdf")]), "report.pdf");
form.append("pages", "1-3");
const res = await fetch("https://bloodweb.net/api/v1/pdf-to-text", {
method: "POST",
headers: { Authorization: `Bearer ${process.env.BLOODWEB_KEY}` },
body: form,
});
if (!res.ok) throw new Error(`${res.status}: ${(await res.json()).error.message}`);
await writeFile("report.txt", Buffer.from(await res.arrayBuffer()));
What you get
Tested 19 October 2026: this exact code ran against our test copy of the API, which runs the same service code as bloodweb.net, and produced this.
It writes report.txt: a text file, 1 KB.
Quarterly report, page 1
A sample page made for the code examples.
Quarterly report, page 2
A sample page made for the code examples.
Quarterly report, page 3
A sample page made for the code examples.
Change the call
Every option the PDF to text takes. Add or change them the same way as the ones in the code.
pages: Pages to read, such as 1-3,5 or 8- (to the end).layout: Keep the physical layout (columns and tables stay lined up). Off gives plain reading order.format: text: one plain-text body, pages separated by form feeds. json: {"pages": [{"page": 1, "text": "..."}]}. One of: text, json.
If it fails
A failed call is not charged. The answer is JSON with an error object; the code above prints its message. The usual ones:
- 401
missing_keyorinvalid_key:BLOODWEB_KEYis not set in the shell that runs the code, or the key was revoked. - 415
unsupported_type: the file is not a type this service reads. The type is read from the file itself, so renaming a file does not change it. - 413
file_too_large: the file is over the limit on the service page. - 402
insufficient_tokens: the balance does not cover the call. Top up in the dashboard. - 429
rate_limited: too many calls at once. Wait the seconds in theRetry-Afterheader and send it again.
Every code and its fix is in the docs.
Next
- The PDF to text page: every option, the limits, and Try it now in your browser.
- Getting started: keys, errors and rate limits for all the services.