pdf-to-png-converter
Version:
Node.js utility to convert PDF file/buffer pages to PNG files/buffers. No build-time compilation required — pre-built native binaries included for all major platforms.
65 lines (64 loc) • 3.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolvePageName = resolvePageName;
exports.finalizePageOutput = finalizePageOutput;
exports.shouldMaterializeContent = shouldMaterializeContent;
exports.processAndSavePage = processAndSavePage;
const flatFilename_js_1 = require("./flatFilename.js");
const pageRenderer_js_1 = require("./pageRenderer.js");
function assertFlatFilename(name, pageNumber) {
if ((0, flatFilename_js_1.containsPathSeparator)(name)) {
throw new Error(`outputFileMaskFunc returned a filename containing a path separator for page ${pageNumber}: "${name}". The filename must be a flat name with no ${flatFilename_js_1.SEPARATOR_DESCRIPTION} characters.`);
}
}
function resolvePageName(pageNumber, defaultMask, outputFileMaskFunc) {
if (outputFileMaskFunc === undefined) {
return `${defaultMask}_page_${pageNumber}.png`;
}
const name = outputFileMaskFunc(pageNumber);
if (typeof name !== 'string') {
throw new Error(`outputFileMaskFunc returned a non-string filename for page ${pageNumber}. Provide a string including the .png extension.`);
}
if (!name) {
throw new Error(`outputFileMaskFunc returned an empty filename for page ${pageNumber}. Provide a non-empty string including the .png extension.`);
}
assertFlatFilename(name, pageNumber);
return name;
}
/**
* Applies the output half of a page's lifecycle to an already-rendered page: pass-through for
* in-memory mode, or sink write + content trimming for file mode. Split out from
* `processAndSavePage` so worker-thread conversions — where rendering happens off-thread but
* output must stay on the main thread (path-security guards live here) — reuse the exact same
* output logic.
*/
async function finalizePageOutput(pageOutput, mode) {
if (mode.kind === 'content') {
return pageOutput;
}
if (pageOutput.content === undefined) {
throw new Error(`Cannot write PNG file "${pageOutput.name}" because content is undefined.`);
}
const resolvedPath = await mode.sink.write(pageOutput.name, pageOutput.content);
const filePageOutput = {
...pageOutput,
kind: 'file',
path: resolvedPath,
content: mode.returnContent ? pageOutput.content : undefined,
};
return filePageOutput;
}
/**
* Returns whether a rendered page must materialize its PNG Buffer. `file` mode always does (the
* bytes are needed for the write); `content` mode only when the caller asked to keep it.
*/
function shouldMaterializeContent(mode) {
return mode.kind === 'file' ? true : mode.returnContent;
}
async function processAndSavePage(pdfDocument, pageName, pageNumber, pageViewportScale, mode) {
if (mode.kind === 'metadata') {
return await (0, pageRenderer_js_1.getPageMetadata)(pdfDocument, pageName, pageNumber, pageViewportScale);
}
const pageOutput = await (0, pageRenderer_js_1.renderPdfPage)(pdfDocument, pageName, pageNumber, pageViewportScale, shouldMaterializeContent(mode));
return await finalizePageOutput(pageOutput, mode);
}