Poppler

Asynchronous node.js wrapper for the Poppler PDF rendering library

README

node-poppler


Asynchronous node.js wrapper for the Poppler PDF rendering library


Overview


Poppler is a PDF rendering library that also includes a collection of utility binaries, which allows for the manipulation and extraction of data from PDF documents such as converting PDF files to HTML, TXT, or PostScript.

The node-poppler module provides an asynchronous node.js wrapper around said utility binaries for easier use.

Installation


Install using npm:

  1. ```bash
  2. npm i node-poppler
  3. ```

Linux and macOS/Darwin support


Windows binaries are provided with this repository.
For Linux users, you will need to download the poppler-data and poppler-utils binaries separately.

An example of downloading the binaries on a Debian system:

  1. ```
  2. sudo apt-get install poppler-data
  3. sudo apt-get install poppler-utils
  4. ```

For macOS users, you can download the latest versions with Homebrew:

  1. ```
  2. brew install poppler
  3. ```

Once they have been installed, you will need to pass the poppler-utils installation directory as a parameter to an instance of the Poppler class:

  1. ```js
  2. const { Poppler } = require("node-poppler");
  3. const poppler = new Poppler("/usr/bin");
  4. ```

API


  1. ```js
  2. const { Poppler } = require("node-poppler");
  3. ```

[API Documentation can be found here](https://github.com/Fdawgs/node-poppler/blob/main/API.md)

Examples


poppler.pdfToCairo


Example of an async await call to poppler.pdfToCairo(), to convert only the first and second page of a PDF file to PNG:

  1. ```js
  2. const { Poppler } = require("node-poppler");

  3. const file = "test_document.pdf";
  4. const poppler = new Poppler();
  5. const options = {
  6. firstPageToConvert: 1,
  7. lastPageToConvert: 2,
  8. pngFile: true,
  9. };
  10. const outputFile = `test_document.png`;

  11. const res = await poppler.pdfToCairo(file, outputFile, options);
  12. console.log(res);
  13. ```

Example of an async await call to poppler.pdfToCairo(), to convert only the first of a PDF file to a new
PDF file using stdout:

  1. ```js
  2. const { writeFile } = require("node:fs/promises");
  3. const { Poppler } = require("node-poppler");

  4. const file = "test_document.pdf";
  5. const poppler = new Poppler();
  6. const options = {
  7. lastPageToConvert: 1,
  8. pdfFile: true,
  9. };

  10. const res = await poppler.pdfToCairo(file, undefined, options);
  11. // pdfToCairo writes to stdout using binary encoding if pdfFile or singleFile options are used
  12. await writeFile("new_file.pdf", res, { encoding: "binary" });
  13. ```

poppler.pdfToHtml


Example of calling poppler.pdfToHtml() with a promise chain:

  1. ```js
  2. const { Poppler } = require("node-poppler");

  3. const file = "test_document.pdf";
  4. const poppler = new Poppler();
  5. const options = {
  6. firstPageToConvert: 1,
  7. lastPageToConvert: 2,
  8. };

  9. poppler
  10. .pdfToHtml(file, undefined, options)
  11. .then((res) => {
  12.   console.log(res);
  13. })
  14. .catch((err) => {
  15.   console.error(err);
  16.   throw err;
  17. });
  18. ```

Example of calling poppler.pdfToHtml() with a promise chain, providing a Buffer as an input:

  1. ```js
  2. const { readFileSync } = require("node:fs");
  3. const { Poppler } = require("node-poppler");

  4. const file = readFileSync("test_document.pdf");
  5. const poppler = new Poppler();
  6. const options = {
  7. firstPageToConvert: 1,
  8. lastPageToConvert: 2,
  9. };

  10. poppler
  11. .pdfToHtml(file, "tester.html", options)
  12. .then((res) => {
  13.   console.log(res);
  14. })
  15. .catch((err) => {
  16.   console.error(err);
  17.   throw err;
  18. });
  19. ```

poppler.pdfToText


Example of calling poppler.pdfToText() with a promise chain:

  1. ```js
  2. const { Poppler } = require("node-poppler");

  3. const file = "test_document.pdf";
  4. const poppler = new Poppler();
  5. const options = {
  6. firstPageToConvert: 1,
  7. lastPageToConvert: 2,
  8. };

  9. poppler
  10. .pdfToText(file, options)
  11. .then((res) => {
  12.   console.log(res);
  13. })
  14. .catch((err) => {
  15.   console.error(err);
  16.   throw err;
  17. });
  18. ```

Contributing


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.

License


node-poppler is licensed under the MIT license.