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