node-poppler

repository·main·Indexed 42 days ago

https://github.com/fdawgs/node-poppler

An asynchronous Node.js wrapper for Poppler PDF rendering utilities (version 11.0.0). It provides a programmatic interface to convert PDF files to HTML, text, or images (via Cairo), as well as utilities to merge PDFs, extract images, list fonts, and manage embedded attachments.

Tokens
8.2K
Snippets
28
Records
30
Agent score
54%

What's inside node-poppler

  1. Install Poppler system dependencies for Linux and macOS

    main

    node-poppler is a wrapper around Poppler utilities. You must install the underlying Poppler binaries on your system.

    Linux (Debian/Ubuntu)

    Install poppler-data and poppler-utils using apt-get:

    sudo apt-get install poppler-data poppler-utils

    macOS

    Install using Homebrew:

    brew install poppler

    Windows

    64-bit Windows binaries are provided via the optional node-poppler-win32 package.

  2. How to cancel Poppler operations

    main

    All methods in the Poppler class accept an extras object as their final argument. You can pass an AbortSignal via extras.signal to cancel the underlying child process execution.

    This is useful for implementing timeouts or responding to user cancellation in long-running PDF processing tasks.

    const controller = new AbortController();
    const { signal } = controller;
    
    // Start the operation
    const promise = poppler.pdfToText('large.pdf', 'out.txt', {}, { signal });
    
    // Cancel the operation after 5 seconds
    setTimeout(() => controller.abort(), 5000);
    
    try {
      await promise;
    } catch (err) {
      if (err.name === 'AbortError') {
        console.log('Operation was cancelled');
      }
    }
  3. Initialize the Poppler class

    main

    To use node-poppler, instantiate the Poppler class. If you do not provide a binPath, the constructor will attempt to find the Poppler pdfinfo binary in your system's PATH.

    On Windows (win32), if local binaries are not found, the package will attempt to use node-poppler-win32 if it is installed.

    If the binaries cannot be located, the constructor will throw an error.

    const { Poppler } = require('node-poppler');
    
    // Option 1: Automatic discovery via PATH
    const poppler = new Poppler();
    
    // Option 2: Explicit path to the directory containing Poppler binaries
    const poppler = new Poppler('/usr/local/bin/poppler-utils');
  4. Use poppler.pdfToCairo to convert PDF to images or other formats

    main

    The pdfToCairo method converts PDF pages to various formats (like PNG) using the Cairo graphics library. You can specify a range of pages to convert.

    Convert specific pages to PNG

    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);

    Convert to PDF via stdout

    If you use pdfFile: true or singleFile: true in the options, the method writes to stdout using binary encoding. You can capture this result and write it to a file using node:fs/promises.

    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);
    await writeFile("new_file.pdf", res, { encoding: "binary", flush: true });
  5. Use poppler.pdfToText to extract text from PDF

    main

    The pdfToText method extracts text content from a PDF file and saves it to a specified output file.

    "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;
    	});
  6. Use poppler.pdfToHtml to convert PDF to HTML

    main

    The pdfToHtml method converts PDF documents to HTML format. It supports both file paths and Buffer inputs.

    Using a file path (CJS)

    "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;
    	});

    Using a Buffer (CJS)

    "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;
    	});
  7. Configure options for pdfattach

    main

    When using the pdfattach command, you can provide the following boolean options to control its behavior:

    • printVersionInfo: (Flag: -v) Prints copyright and version information.
    • replace: (Flag: -replace) Replaces an embedded file if a file with the same name already exists.
    // Example usage of options in a pdfattach call
    // Note: Exact syntax depends on the parent API implementation
    {
      printVersionInfo: true,
      replace: true
    }
  8. Convert PDF to PostScript with pdfToPs

    main

    Use pdfToPs to convert a PDF file to PostScript (PS).

    Parameters:

    • file: PDF file as a Buffer or a filepath string.
    • outputFile: Filepath of the file to output the results to. If undefined, it writes to stdout.
    • options: PdfToPsOptions.
    • extras: PopplerExtraOptions.
    const { Poppler } = require('node-poppler');
    const poppler = new Poppler();
    
    await poppler.pdfToPs('input.pdf', 'output.ps', { /* PdfToPsOptions */ });
  9. Convert PDF to PPM images with pdfToPpm

    main

    Use pdfToPpm to convert a PDF file to color (PPM), grayscale (PGM), or monochrome (PBM) image files.

    Parameters:

    • file: PDF file as a Buffer or a filepath string.
    • outputPath: Filepath to output the results to.
    • options: PdfToPpmOptions.
    • extras: PopplerExtraOptions.
    const { Poppler } = require('node-poppler');
    const poppler = new Poppler();
    
    await poppler.pdfToPpm('input.pdf', 'output_path', { /* PdfToPpmOptions */ });
  10. Convert PDF to images/vectors with pdfToCairo

    main

    Use pdfToCairo to convert a PDF file to EPS, JPEG, PDF, PNG, PS, SVG, or TIFF.

    Parameters:

    • file: PDF file as a Buffer or a filepath string.
    • outputFile: Filepath of the file to output the results to. If undefined, it writes to stdout.
    • options: PdfToCairoOptions.
    • extras: PopplerExtraOptions.

    Stdout Behavior: Using stdout is only valid with image formats (jpeg, png, tiff) if options.singleFile is set to true. If outputFile is undefined and singleFile or pdfFile is true, the encoding is set to binary to prevent corruption.

    const { Poppler } = require('node-poppler');
    const poppler = new Poppler();
    
    // Convert to PNG file
    await poppler.pdfToCairo('input.pdf', 'output.png', { /* PdfToCairoOptions */ });
  11. Convert PDF to Text with pdfToText

    main

    Use pdfToText to convert a PDF file to TXT.

    Parameters:

    • file: PDF file as a Buffer or a filepath string.
    • outputFile: Filepath of the file to output the results to. If undefined, it writes to stdout.
    • options: PdfToTextOptions. Note that options.maintainLayout is internally mapped to the --preserveWhitespace flag.
    • extras: PopplerExtraOptions.
    const { Poppler } = require('node-poppler');
    const poppler = new Poppler();
    
    // Extract text to stdout
    const text = await poppler.pdfToText('input.pdf', undefined, { maintainLayout: true });