Asynchronous Node.js wrapper for the Poppler PDF rendering utilities
Poppler is a PDF rendering library that also includes a collection of utilities, which provide functionality for manipulating PDF documents and extracting data from them, including converting PDF files to HTML, TXT, or PostScript.
The node-poppler module provides an asynchronous Node.js wrapper around these utilities for easier use.
Install using npm:
npm i node-poppler64-bit Windows binaries are provided via an optional dependency on the node-poppler-win32 package.
For Linux and macOS users, the poppler-data package and poppler-utils binaries will need to be installed separately.
An example of downloading the binaries on a Debian system:
sudo apt-get install poppler-data poppler-utilsFor macOS users, the binaries can be installed with Homebrew:
brew install popplerPlease refer to the JSDoc comments in the source code or the generated type definitions for information on the available options.
Example of an async/await call to poppler.pdfToCairo() to convert the first two pages of a PDF file to PNG using ESM syntax:
import { Poppler } from "node-poppler";
const file = "test_document.pdf";
const poppler = new Poppler();
const options = {
firstPageToConvert: 1,
lastPageToConvert: 2,
pngFile: true,
};
const outputFile = "test_document";
const res = await poppler.pdfToCairo(file, outputFile, options);
console.log(res);Example of an async/await call to poppler.pdfToCairo() to convert the first page of a PDF file to a new PDF file via stdout using ESM syntax:
import { writeFile } from "node:fs/promises";
import { Poppler } from "node-poppler";
const file = "test_document.pdf";
const poppler = new Poppler();
const options = {
lastPageToConvert: 1,
pdfFile: true,
};
const res = await poppler.pdfToCairo(file, undefined, options);
// pdfToCairo writes to stdout using binary encoding if pdfFile or singleFile options are used
await writeFile("new_file.pdf", res, { encoding: "binary", flush: true });Example of calling poppler.pdfToHtml() with a promise chain using CJS syntax:
"use strict";
const { Poppler } = require("node-poppler");
const file = "test_document.pdf";
const poppler = new Poppler();
const options = {
firstPageToConvert: 1,
lastPageToConvert: 2,
};
poppler
.pdfToHtml(file, undefined, options)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.error(err);
throw err;
});Example of calling poppler.pdfToHtml() with a promise chain using CJS syntax and a Buffer as input:
"use strict";
const { readFileSync } = require("node:fs");
const { Poppler } = require("node-poppler");
const file = readFileSync("test_document.pdf");
const poppler = new Poppler();
const options = {
firstPageToConvert: 1,
lastPageToConvert: 2,
};
poppler
.pdfToHtml(file, "tester.html", options)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.error(err);
throw err;
});Example of calling poppler.pdfToText() with a promise chain using CJS syntax:
"use strict";
const { Poppler } = require("node-poppler");
const file = "test_document.pdf";
const poppler = new Poppler();
const options = {
firstPageToConvert: 1,
lastPageToConvert: 2,
};
const outputFile = "test_document.txt";
poppler
.pdfToText(file, outputFile, options)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.error(err);
throw err;
});Contributions are welcome, and any help is greatly appreciated!
See the contributing guide for details on how to get started. Please adhere to this project's Code of Conduct when contributing.
- Albert Astals Cid - Poppler developer
node-poppler is licensed under the MIT license.