Socket
Socket
Sign inDemoInstall

pdf-to-png-converter

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pdf-to-png-converter - npm Package Compare versions

Comparing version 2.7.1 to 3.0.0

out/compare.png.d.ts

2

out/const.d.ts

@@ -1,3 +0,3 @@

import { PdfToPngOptions } from "./types/pdf.to.png.options";
import { PdfToPngOptions } from './types/pdf.to.png.options';
export declare const PDF_TO_PNG_OPTIONS_DEFAULTS: PdfToPngOptions;
//# sourceMappingURL=const.d.ts.map

@@ -8,2 +8,3 @@ "use strict";

useSystemFonts: false,
enableXfa: false,
outputFileMask: 'buffer',

@@ -10,0 +11,0 @@ strictPagesToProcess: false,

@@ -7,7 +7,7 @@ "use strict";

exports.NodeCanvasFactory = void 0;
const assert_1 = require("assert");
const node_assert_1 = require("node:assert");
const canvas_1 = __importDefault(require("canvas"));
class NodeCanvasFactory {
create(width, height) {
(0, assert_1.strict)(width > 0 && height > 0, 'Invalid canvas size');
(0, node_assert_1.strict)(width > 0 && height > 0, 'Invalid canvas size');
const canvas = canvas_1.default.createCanvas(width, height);

@@ -21,4 +21,4 @@ const context = canvas.getContext('2d');

reset(canvasAndContext, width, height) {
(0, assert_1.strict)(canvasAndContext.canvas, 'Canvas is not specified');
(0, assert_1.strict)(width > 0 && height > 0, 'Invalid canvas size');
(0, node_assert_1.strict)(canvasAndContext.canvas, 'Canvas is not specified');
(0, node_assert_1.strict)(width > 0 && height > 0, 'Invalid canvas size');
canvasAndContext.canvas.width = width;

@@ -28,3 +28,3 @@ canvasAndContext.canvas.height = height;

destroy(canvasAndContext) {
(0, assert_1.strict)(canvasAndContext.canvas, 'Canvas is not specified');
(0, node_assert_1.strict)(canvasAndContext.canvas, 'Canvas is not specified');
canvasAndContext.canvas.width = 0;

@@ -31,0 +31,0 @@ canvasAndContext.canvas.height = 0;

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pdfToPng = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const pdfjs = __importStar(require("pdfjs-dist/legacy/build/pdf"));
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const pdf_1 = require("pdfjs-dist/legacy/build/pdf");
const const_1 = require("./const");

@@ -35,3 +12,3 @@ const node_canvas_factory_1 = require("./node.canvas.factory");

const isBuffer = Buffer.isBuffer(pdfFilePathOrBuffer);
if (!isBuffer && !(0, fs_1.existsSync)(pdfFilePathOrBuffer)) {
if (!isBuffer && !(0, node_fs_1.existsSync)(pdfFilePathOrBuffer)) {
throw Error(`PDF file not found on: ${pdfFilePathOrBuffer}.`);

@@ -41,7 +18,8 @@ }

? pdfFilePathOrBuffer
: (0, fs_1.readFileSync)(pdfFilePathOrBuffer);
: (0, node_fs_1.readFileSync)(pdfFilePathOrBuffer);
const pdfDocInitParams = (0, props_to_pdf_doc_init_params_1.propsToPdfDocInitParams)(props);
pdfDocInitParams.data = new Uint8Array(pdfFileBuffer);
const pdfDocument = await pdfjs.getDocument(pdfDocInitParams).promise;
const pngPagesOutput = [];
const canvasFactory = new node_canvas_factory_1.NodeCanvasFactory();
pdfDocInitParams.canvasFactory = canvasFactory;
const pdfDocument = await (0, pdf_1.getDocument)(pdfDocInitParams).promise;
const targetedPageNumbers = props?.pagesToProcess !== undefined

@@ -56,5 +34,16 @@ ? props.pagesToProcess

}
if (props?.outputFolder && !(0, fs_1.existsSync)(props.outputFolder)) {
(0, fs_1.mkdirSync)(props.outputFolder, { recursive: true });
if (props?.outputFolder && !(0, node_fs_1.existsSync)(props.outputFolder)) {
(0, node_fs_1.mkdirSync)(props.outputFolder, { recursive: true });
}
let pageName;
if (props?.outputFileMask) {
pageName = props.outputFileMask;
}
if (!pageName && !isBuffer) {
pageName = (0, node_path_1.parse)(pdfFilePathOrBuffer).name;
}
if (!pageName) {
pageName = const_1.PDF_TO_PNG_OPTIONS_DEFAULTS.outputFileMask;
}
const pngPagesOutput = [];
for (const pageNumber of targetedPageNumbers) {

@@ -70,3 +59,2 @@ if (pageNumber > pdfDocument.numPages || pageNumber < 1) {

});
const canvasFactory = new node_canvas_factory_1.NodeCanvasFactory();
const canvasAndContext = canvasFactory.create(viewport.width, viewport.height);

@@ -76,23 +64,17 @@ const renderContext = {

viewport,
canvasFactory,
};
await page.render(renderContext).promise;
let pageName;
if (props?.outputFileMask) {
pageName = props.outputFileMask;
}
if (!pageName && !isBuffer) {
pageName = (0, path_1.parse)(pdfFilePathOrBuffer).name;
}
if (!pageName) {
pageName = const_1.PDF_TO_PNG_OPTIONS_DEFAULTS.outputFileMask;
}
const pngPageOutput = {
pageNumber,
name: `${pageName}_page_${pageNumber}.png`,
content: canvasAndContext.canvas.toBuffer(),
path: '',
width: viewport.width,
height: viewport.height,
};
canvasFactory.destroy(canvasAndContext);
page.cleanup();
if (props?.outputFolder) {
pngPageOutput.path = (0, path_1.resolve)(props.outputFolder, pngPageOutput.name);
(0, fs_1.writeFileSync)(pngPageOutput.path, pngPageOutput.content);
pngPageOutput.path = (0, node_path_1.resolve)(props.outputFolder, pngPageOutput.name);
(0, node_fs_1.writeFileSync)(pngPageOutput.path, pngPageOutput.content);
}

@@ -99,0 +81,0 @@ pngPagesOutput.push(pngPageOutput);

@@ -9,5 +9,7 @@ "use strict";

const cMapPacked = true;
const standardFontDataUrl = '../node_modules/pdfjs-dist/standard_fonts/';
const pdfDocInitParams = {
cMapUrl,
cMapPacked,
standardFontDataUrl,
};

@@ -19,2 +21,4 @@ pdfDocInitParams.verbosity = props?.verbosityLevel !== undefined ? props?.verbosityLevel : verbosity_level_1.VerbosityLevel.ERRORS;

props?.useSystemFonts !== undefined ? props.useSystemFonts : const_1.PDF_TO_PNG_OPTIONS_DEFAULTS.useSystemFonts;
pdfDocInitParams.enableXfa =
props?.enableXfa !== undefined ? props.enableXfa : const_1.PDF_TO_PNG_OPTIONS_DEFAULTS.enableXfa;
pdfDocInitParams.password =

@@ -21,0 +25,0 @@ props?.pdfFilePassword !== undefined ? props?.pdfFilePassword : const_1.PDF_TO_PNG_OPTIONS_DEFAULTS.pdfFilePassword;

@@ -5,2 +5,3 @@ export type PdfToPngOptions = {

useSystemFonts?: boolean;
enableXfa?: boolean;
pdfFilePassword?: string;

@@ -7,0 +8,0 @@ outputFolder?: string;

/// <reference types="node" />
export type PngPageOutput = {
pageNumber: number;
name: string;
content: Buffer;
path: string;
width: number;
height: number;
};
//# sourceMappingURL=png.page.output.d.ts.map
{
"name": "pdf-to-png-converter",
"version": "2.7.1",
"version": "3.0.0",
"description": "Node.js utility to convert PDF file/buffer pages to PNG files/buffers with no native dependencies.",

@@ -32,3 +32,3 @@ "keywords": [

"predocker:run": "npm run clean",
"docker:run": "docker run --rm -it -v test-results:/usr/pkg/test-results test-pdf-to-png-converter",
"docker:run": "docker run --rm -it -v $PWD/test-results:/usr/pkg/test-results test-pdf-to-png-converter",
"docker:test": "jest",

@@ -44,17 +44,18 @@ "license-checker": "npx license-checker --production --onlyAllow 'MIT; MIT OR X11; BSD; ISC; Apache-2.0; Unlicense'",

"dependencies": {
"canvas": "^2.10.2",
"pdfjs-dist": "^3.1.81"
"canvas": "^2.11.2",
"pdfjs-dist": "^3.5.141"
},
"devDependencies": {
"@types/jest": "^29.2.3",
"@types/node": "^18.11.9",
"@typescript-eslint/eslint-plugin": "^5.44.0",
"@typescript-eslint/parser": "^5.44.0",
"eslint": "^8.28.0",
"jest": "^29.3.1",
"png-visual-compare": "^0.5.1",
"ts-jest": "^29.0.3",
"@types/jest": "^29.5.0",
"@types/node": "^18.15.11",
"@typescript-eslint/eslint-plugin": "^5.57.1",
"@typescript-eslint/parser": "^5.57.1",
"eslint": "^8.38.0",
"jest": "^29.5.0",
"png-visual-compare": "^1.2.0",
"rimraf": "^4.4.1",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.3"
"typescript": "^5.0.4"
}
}

@@ -33,2 +33,3 @@ # pdf-to-png-converter

useSystemFonts: false, // When `true`, fonts that aren't embedded in the PDF document will fallback to a system font. Default value is false.
enableXfa: false, // Render Xfa forms if any. Default value is false.
viewportScale: 2.0, // The desired scale of PNG viewport. Default value is 1.0.

@@ -39,3 +40,3 @@ outputFolder: 'output/folder', // Folder to write output PNG files. If not specified, PNG output will be available only as a Buffer content, without saving to a file.

pagesToProcess: [1, 3, 11], // Subset of pages to convert (first page = 1), other pages will be skipped if specified.
strictPagesToProcess: false // When `true`, will throw an error if specified page number in pagesToProcess is invalid, otherwise will skip invalid page. Default value is false.
strictPagesToProcess: false, // When `true`, will throw an error if specified page number in pagesToProcess is invalid, otherwise will skip invalid page. Default value is false.
verbosityLevel: 0 // Verbosity level. ERRORS: 0, WARNINGS: 1, INFOS: 5. Default value is 0.

@@ -51,5 +52,8 @@ });

{
name: string; // PNG page name in a format `{pdfFileName}_page_{pdfPageNumber}.png`,
pageNumber: number; // Page number in PDF file
name: string; // PNG page name in a format `{pdfFileName}_page_{pdfPageNumber}.png`
content: Buffer; // PNG page Buffer content
path: string; // Path to the rendered PNG page file (empty string and if outputFilesFolder is not provided)
width: number; // PNG page width
height: number; // PNG page height
}

@@ -56,0 +60,0 @@ ```

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc