Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-poppler

Package Overview
Dependencies
Maintainers
1
Versions
74
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-poppler - npm Package Compare versions

Comparing version 1.4.0 to 1.4.1

undefined

6

.eslintrc.json

@@ -6,3 +6,3 @@ {

},
"extends": ["eslint:recommended", "plugin:jest/recommended", "prettier"],
"extends": ["airbnb-base", "plugin:jest/recommended", "prettier"],
"parserOptions": {

@@ -30,6 +30,4 @@ "ecmaVersion": 2020,

"no-console": "off",
"prefer-destructuring": "off",
"prefer-const": "error",
"no-var": "error"
"prefer-destructuring": "off"
}
}
{
"name": "node-poppler",
"version": "1.4.0",
"version": "1.4.1",
"description": "Node.js wrapper for the Poppler PDF rendering library",

@@ -15,4 +15,4 @@ "keywords": [

"jest": "jest src --coverage",
"lint": "eslint .",
"prettier": "prettier src/*.js --write",
"lint": "eslint src/**/*.js",
"prettier": "prettier src/**/*.js --write",
"test": "yarn lint && yarn test-coverage",

@@ -46,2 +46,3 @@ "test-coverage": "jest --coverage --verbose",

"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-config-prettier": "^6.9.0",

@@ -48,0 +49,0 @@ "eslint-plugin-import": "^2.19.1",

@@ -84,2 +84,51 @@ const os = require('os');

* @author Frazer Smith
* @description Embeds files (attachments) into a PDF file.
* @param {Object=} options
* @param {Boolean=} options.printVersionInfo - Print copyright and version info.
* @param {Boolean=} options.replace - Replace embedded file with same name (if it exists).
* @param {String} file - Filepath of the PDF file to read.
* @param {String} fileToAttach - Filepath of the attachment to be embedded into the PDF file.
* @param {String} outputFile - Filepath of the file to output the results to.
* @returns {Promise}
*/
pdfAttach(options, file, fileToAttach, outputFile) {
return new Promise((resolve, reject) => {
const acceptedOptions = {
printVersionInfo: { arg: '-v', type: 'boolean' },
replace: { arg: '-replace', type: 'boolean' }
};
// Build array of args based on options passed
const args = [];
/**
* Check each option provided is valid and of the correct type,
* before adding it to argument list.
*/
if (options) {
parseOptions(options, acceptedOptions, args).catch((err) => {
reject(err);
});
}
args.push(file);
args.push(fileToAttach);
args.push(outputFile);
execFile(
path.join(this.popplerPath, 'pdfattach'),
args,
(err, stdout) => {
if (err) {
reject(err);
} else {
resolve(stdout);
}
}
);
});
}
/**
* @author Frazer Smith
* @description Lists or extracts embedded files (attachments) from a PDF file.

@@ -206,2 +255,83 @@ *

* @author Frazer Smith
* @description prints the contents of the ´Info' dictionary from a PDF file.
*
* @param {Object=} options
* @param {Number=} options.firstPageToConvert - First page to print.
* @param {Number=} options.lastPageToConvert - Last page to print.
* @param {Boolean=} options.listEncodingOptions - List the available encodings.
* @param {String=} options.outputEncoding - Sets the encoding to use for text output.
* This defaults to "UTF-8".
* @param {String=} options.ownerPassword - Owner password (for encrypted files).
* @param {Boolean=} options.printBoundingBoxes - Prints the page box bounding boxes:
* MediaBox, CropBox, BleedBox, TrimBox, and ArtBox.
* @param {Boolean=} options.printDocStruct - Prints the logical document structure
* of a Tagged-PDF file.
* @param {Boolean=} options.printDocStructText - Print the textual content along with the
* document structure of a Tagged-PDF file. Note that extracting text this way might be slow
*
* for big PDF files.
* @param {Boolean=} options.printIsoDates - Prints dates in ISO-8601 format (including the time zone).
* @param {Boolean=} options.printJS - Prints all JavaScript in the PDF file.
* @param {Boolean=} options.printMetadata - Prints document-level metadata. (This is the "Metadata"
* stream from the PDF file's Catalog object).
* @param {Boolean=} options.printNamedDests - Print a list of all named destinations. If a page range
* is specified using the 'firstPageToConvert' and 'lastPageToConvert' options, only destinations
* in the page range are listed.
* @param {Boolean=} options.printRawDates - Prints the raw (undecoded) date strings, directly from the PDF file.
* @param {Boolean=} options.printVersionInfo - Print copyright and version info.
* @param {String=} options.userPassword - User password (for encrypted files).
* @param {String} file - Filepath of the PDF file to read.
* @returns {Promise}
*/
pdfInfo(options, file) {
return new Promise((resolve, reject) => {
const acceptedOptions = {
firstPageToConvert: { arg: '-f', type: 'number' },
lastPageToConvert: { arg: '-l', type: 'number' },
listEncodingOptions: { arg: '-listenc', type: 'boolean' },
outputEncoding: { arg: '-enc', type: 'string' },
ownerPassword: { arg: '-opw', type: 'string' },
printBoundingBoxes: { arg: '-box', type: 'boolean' },
printDocStruct: { arg: '-struct', type: 'boolean' },
printDocStructText: { arg: '-struct-text', type: 'boolean' },
printIsoDates: { arg: '-isodates', type: 'boolean' },
printJS: { arg: '-js', type: 'boolean' },
printMetadata: { arg: '-meta', type: 'boolean' },
printNamedDests: { arg: '-dests', type: 'boolean' },
printRawDates: { arg: '-rawdates', type: 'boolean' },
printVersionInfo: { arg: '-v', type: 'boolean' },
userPassword: { arg: '-upw', type: 'string' }
};
// Build array of args based on options passed
const args = [];
/**
* Check each option provided is valid and of the correct type,
* before adding it to argument list.
*/
if (options) {
parseOptions(options, acceptedOptions, args).catch((err) => {
reject(err);
});
}
args.push(file);
execFile(
path.join(this.popplerPath, 'pdfinfo'),
args,
(err, stdout) => {
if (err) {
reject(err);
} else {
resolve(stdout);
}
}
);
});
}
/**
* @author Frazer Smith
* @description Extract single pages from a PDF file,

@@ -263,2 +393,174 @@ * and writes one PDF file for each page to outputPattern.

* @author Frazer Smith
* @description Converts PDF to PNG/JPEG/TIFF/PDF/PS/EPS/SVG.
*
* @param {Object} options
* @param {String=} options.antialias Set the cairo antialias option used for text
* and drawing in image files (or rasterized regions in vector output).
* Options are: default; none; gray; subpixel; fast; good; best.
* @param {Boolean=} options.cropBox - Uses the crop box rather than media box when
* generating the files (PNG/JPEG/TIFF only).
* @param {Number=} options.cropHeight - Specifies the height of crop area in pixels
* (image output) or points (vector output).
* @param {Number=} options.cropSize - Specifies the size of crop square in pixels
* (image output) or points (vector output).
* @param {Number=} options.cropWidth - Specifies the width of crop area in pixels
* (image output) or points (vector output).
* @param {Number=} options.cropXAxis - Specifies the x-coordinate of the crop area top left
* corner in pixels (image output) or points (vector output).
* @param {Number=} options.cropYAxis - Specifies the y-coordinate of the crop area top left
* corner in pixels (image output) or points (vector output).
* @param {Boolean=} options.duplex - Adds the %%IncludeFeature: *Duplex DuplexNoTumble DSC
* comment to the PostScript file (PS only). This tells the print manager to enable duplexing.
* @param {Boolean=} options.epsFile - Generate an EPS file. An EPS file contains a single image,
* so if you use this option with a multi-page PDF file, you must use 'firstPageToConvert' and
* 'lastPageToConvert' to specify a single page.
* The page size options (originalPageSizes, paperSize, paperWidth, paperHeight) can not be used
* with this option.
* @param {Boolean=} options.evenPagesOnly - Generates only the even numbered pages.
* @param {Boolean=} options.fillPage - Expand PDF pages smaller than the paper to fill the
* paper (PS,PDF,SVG only). By default, these pages are not scaled.
* @param {Number=} options.firstPageToConvert - Specifies the first page to convert.
* @param {Boolean=} options.grayscaleFile - Generate a grayscale file (PNG, JPEG, and TIFF only).
* @param {Boolean=} options.iccFile - Use the specified ICC file as the output profile
* (PNG only). The profile will be embedded in the PNG file.
* @param {Boolean=} options.jpegFile - Generates a JPEG file(s).
* @param {Number=} options.lastPageToConvert - Specifies the last page to convert.
* @param {Boolean=} options.monochromeFile - Generate a monochrome file (PNG and TIFF only).
* @param {Boolean=} options.noCenter - By default, PDF pages smaller than the paper
* (after any scaling) are centered on the paper. This option causes them to be aligned to
* the lower-left corner of the paper instead (PS,PDF,SVG only).
* @param {Boolean=} options.noCrop - By default, printing output is cropped to the CropBox
* specified in the PDF file. This option disables cropping (PS, PDF, SVG only).
* @param {Boolean=} options.noShrink - Don't scale PDF pages which are larger than the paper
* (PS,PDF,SVG only). By default, pages larger than the paper are shrunk to fit.
* @param {Boolean=} options.oddPagesOnly - Generates only the odd numbered pages.
* @param {Boolean=} options.originalPageSizes - Set the paper size of each page to match
* the size specified in the PDF file.
* @param {String=} options.ownerPassword - Specify the owner password for the PDF file.
* Providing this will bypass all security restrictions.
* @param {Number=} options.paperHeight - Set the paper height, in points (PS, PDF, SVG only).
* @param {String=} options.paperSize - Set the paper size to one of "letter", "legal", "A4",
* or "A3" (PS,PDF,SVG only). This can also be set to "match", which will set the paper size
* of each page to match the size specified in the PDF file. If none of the paperSize,
* paperWidth, or paperHeight options are specified the default is to match the paper size.
* @param {Number=} options.paperWidth - Set the paper width, in points (PS,PDF,SVG only).
* @param {Boolean=} options.pdfFile - Generates a PDF file.
* @param {Boolean=} options.pngFile - Generates a PNG file(s).
* @param {Boolean=} options.printVersionInfo - Print copyright and version information.
* @param {Boolean=} options.psFile - Generate a PS file.
* @param {Boolean=} options.psLevel2 - Generate Level 2 PostScript (PS only).
* @param {Boolean=} options.psLevel3 - Generate Level 3 PostScript (PS only). This enables all
* Level 2 features plus shading patterns and masked images. This is the default setting.
* @param {Boolean=} options.quiet - Don't print any messages or errors.
* @param {Number=} options.resolutionXAxis - Specifies the X resolution, in pixels per inch of
* image files (or rasterized regions in vector output). The default is 150 PPI.
* @param {Number=} options.resolutionXYAxis - Specifies the X and Y resolution, in pixels per
* inch of image files (or rasterized regions in vector output). The default is 150 PPI.
* @param {Number=} options.resolutionYAxis - Specifies the Y resolution, in pixels per inch of
* image files (or rasterized regions in vector output). The default is 150 PPI.
* @param {Number=} options.scalePageTo - Scales the long side of each page (width for landscape
* pages, height for portrait pages) to fit in scale-to pixels. The size of the short side will
* be determined by the aspect ratio of the page (PNG/JPEG/TIFF only).
* @param {Number=} options.scalePageToXAxis - Scales each page horizontally to fit in scale-to-x
* pixels. If scale-to-y is set to -1, the vertical size will determined by the aspect ratio of
* the page (PNG/JPEG/TIFF only).
* @param {Number=} options.scalePageToYAxis - Scales each page vertically to fit in scale-to-y
* pixels. If scale-to-x is set to -1, the horizontal size will determined by the aspect ratio of
* the page (PNG/JPEG/TIFF only).
* @param {Boolean=} options.singleFile - Writes only the first page and does not add digits.
* @param {Boolean=} options.svgFile - Generate a SVG (Scalable Vector Graphics) file.
* @param {String=} options.tiffCompression - Set TIFF compression to one of "none", "packbits",
* "jpeg", "lzw", or "deflate".
* @param {Boolean=} options.tiffFile - Generates a TIFF file(s).
* @param {Boolean=} options.transparentPageColor - Use a transparent page color
* instead of white (PNG and TIFF only).
* @param {String=} options.userPassword - Specify the user password for the PDF file.
* @param {String} file - Filepath of the PDF file to read.
* @param {String=} outputFile - Filepath of the file to output the results to.
* @returns {Promise}
*/
pdfToCairo(options, file, outputFile) {
return new Promise((resolve, reject) => {
const acceptedOptions = {
antialias: { arg: '-antialias', type: 'string' },
cropBox: { arg: '-cropbox', type: 'boolean' },
cropHeight: { arg: '-H', type: 'number' },
cropSize: { arg: '-sz', type: 'number' },
cropWidth: { arg: '-W', type: 'number' },
cropXAxis: { arg: '-x', type: 'number' },
cropYAxis: { arg: '-y', type: 'number' },
duplex: { arg: '-duplex', type: 'boolean' },
epsFile: { arg: '-eps', type: 'boolean' },
evenPagesOnly: { arg: '-e', type: 'boolean' },
fillPage: { arg: '-expand', type: 'boolean' },
firstPageToConvert: { arg: '-f', type: 'number' },
grayscaleFile: { arg: '-gray', type: 'boolean' },
iccFile: { arg: '-icc', type: 'string' },
jpegFile: { arg: '-jpeg', type: 'boolean' },
lastPageToConvert: { arg: '-l', type: 'number' },
monochromeFile: { arg: '-mono', type: 'boolean' },
noCenter: { arg: '-nocenter', type: 'boolean' },
noCrop: { arg: '-nocrop', type: 'boolean' },
noShrink: { arg: '-noshrink', type: 'boolean' },
oddPagesOnly: { arg: '-o', type: 'boolean' },
originalPageSizes: { arg: '-origpagesizes', type: 'boolean' },
ownerPassword: { arg: '-opw', type: 'string' },
paperHeight: { arg: '-paperh', type: 'number' },
paperSize: { arg: '-paper', type: 'string' },
paperWidth: { arg: '-paperw', type: 'number' },
pdfFile: { arg: '-pdf', type: 'boolean' },
pngFile: { arg: '-png', type: 'boolean' },
printVersionInfo: { arg: '-v', type: 'boolean' },
psFile: { arg: '-ps', type: 'boolean' },
psLevel2: { arg: '-level2', type: 'boolean' },
psLevel3: { arg: '-level3', type: 'boolean' },
quiet: { arg: '-q', type: 'boolean' },
resolutionXAxis: { arg: '-rx', type: 'number' },
resolutionXYAxis: { arg: '-r', type: 'number' },
resolutionYAxis: { arg: '-ry', type: 'number' },
scalePageTo: { arg: '-scale-to', type: 'number' },
scalePageToXAxis: { arg: '-scale-to-x', type: 'number' },
scalePageToYAxis: { arg: '-scale-to-y', type: 'number' },
singleFile: { arg: '-singlefile', type: 'boolean' },
svgFile: { arg: '-svg', type: 'boolean' },
tiffCompression: { arg: '-tiffcompression', type: 'string' },
tiffFile: { arg: '-tiff', type: 'boolean' },
transparentPageColor: { arg: '-transp', type: 'boolean' },
userPassword: { arg: '-upw', type: 'string' }
};
// Build array of args based on options passed
const args = [];
/**
* Check each option provided is valid and of the correct type,
* before adding it to argument list.
*/
if (options) {
parseOptions(options, acceptedOptions, args).catch((err) => {
reject(err);
});
}
args.push(file);
if (outputFile) {
args.push(outputFile);
}
execFile(
path.join(this.popplerPath, 'pdftocairo'),
args,
(err, stdout) => {
if (err) {
reject(err);
} else {
resolve(stdout);
}
}
);
});
}
/**
* @author Frazer Smith
* @description Converts PDF file to HTML.

@@ -304,3 +606,3 @@ * Poppler will use the directory and name of the original file

exchangePdfLinks: { arg: '-p', type: 'boolean' },
extractHidden: { arg: '', type: 'boolean' },
extractHidden: { arg: '-hidden', type: 'boolean' },
firstPageToConvert: { arg: '-f', type: 'number' },

@@ -358,8 +660,11 @@ fontFullName: { arg: '-fontfullname', type: 'boolean' },

* @author Frazer Smith
* @description Converts PDF to PNG/JPEG/TIFF/PDF/PS/EPS/SVG.
* @description Converts PDF to to colour image files in Portable Pixmap (PPM) format,
* grayscale image files in Portable Graymap (PGM) format, or monochrome image files
* in Portable Bitmap (PBM) format.
*
* @param {Object} options
* @param {String=} options.antialias Set the cairo antialias option used for text
* and drawing in image files (or rasterized regions in vector output).
* Options are: default; none; gray; subpixel; fast; good; best
* @param {String=} options.antialiasFonts - Enable or disable font anti-aliasing.
* This defaults to "yes".
* @param {String=} options.antialiasVectors - Enable or disable vector anti-aliasing.
* This defaults to "yes".
* @param {Boolean=} options.cropBox - Uses the crop box rather than media box when

@@ -377,43 +682,15 @@ * generating the files (PNG/JPEG/TIFF only).

* corner in pixels (image output) or points (vector output).
* @param {Boolean=} options.duplex - Adds the %%IncludeFeature: *Duplex DuplexNoTumble DSC
* comment to the PostScript file (PS only). This tells the print manager to enable duplexing.
* @param {Boolean=} options.epsFile - Generate an EPS file. An EPS file contains a single image,
* so if you use this option with a multi-page PDF file, you must use -f and -l to specify
* a single page. The page size options (originalPageSize, paperSize, paperWidth,
* paperHeight) can not be used with this option.
* @param {Boolean=} options.evenPagesOnly - Generates only the even numbered pages.
* @param {Boolean=} options.fillPage - Expand PDF pages smaller than the paper to fill the
* paper (PS,PDF,SVG only). By default, these pages are not scaled.
* @param {Number=} options.firstPageToConvert - Specifies the first page to convert.
* @param {Boolean=} options.grayscaleFile - Generate a grayscale file (PNG, JPEG, and TIFF only).
* @param {Boolean=} options.iccFile - Use the specified ICC file as the output profile
* (PNG only). The profile will be embedded in the PNG file.
* @param {Boolean=} options.jpegFile - Generates a JPEG file(s).
* @param {String=} options.freetype - Enable or disable FreeType (a TrueType / Type 1 font rasterizer).
* This defaults to "yes".
* @param {Boolean=} options.grayscaleFile - Generate a grayscale PGM file (instead of a color PPM file).
* @param {Boolean=} options.jpegFile - Generates a JPEG file instead a PPM file.
* @param {Number=} options.lastPageToConvert - Specifies the last page to convert.
* @param {Boolean=} options.monochromeFile - Generate a monochrome file (PNG and TIFF only).
* @param {Boolean=} options.noCenter - By default, PDF pages smaller than the paper
* (after any scaling) are centered on the paper. This option causes them to be aligned to
* the lower-left corner of the paper instead (PS,PDF,SVG only).
* @param {Boolean=} options.noCrop - By default, printing output is cropped to the CropBox
* specified in the PDF file. This option disables cropping (PS, PDF, SVG only).
* @param {Boolean=} options.noShrink - Don't scale PDF pages which are larger than the paper
* (PS,PDF,SVG only). By default, pages larger than the paper are shrunk to fit.
* @param {Boolean=} options.monochromeFile - Generate a monochrome PBM file (instead of a color PPM file).
* @param {Boolean=} options.oddPagesOnly - Generates only the odd numbered pages.
* @param {Boolean=} options.originalPageSizes - Set the paper size of each page to match
* the size specified in the PDF file.
* @param {String=} options.ownerPassword - Specify the owner password for the PDF file.
* Providing this will bypass all security restrictions.
* @param {Number=} options.paperHeight - Set the paper height, in points (PS, PDF, SVG only).
* @param {String=} options.paperSize - Set the paper size to one of "letter", "legal", "A4",
* or "A3" (PS,PDF,SVG only). This can also be set to "match", which will set the paper size
* of each page to match the size specified in the PDF file. If none of the paperSize,
* paperWidth, or paperHeight options are specified the default is to match the paper size.
* @param {Number=} options.paperWidth - Set the paper width, in points (PS,PDF,SVG only).
* @param {Boolean=} options.pdfFile - Generates a PDF file.
* @param {Boolean=} options.pngFile - Generates a PNG file(s).
* @param {Boolean=} options.pngFile - Generates a PNG file instead a PPM file.
* @param {Boolean=} options.printVersionInfo - Print copyright and version information.
* @param {Boolean=} options.psFile - Generate a PS file.
* @param {Boolean=} options.psLevel2 - Generate Level 2 PostScript (PS only).
* @param {Boolean=} options.psLevel3 - Generate Level 3 PostScript (PS only). This enables all
* Level 2 features plus shading patterns and masked images. This is the default setting.
* @param {Boolean=} options.quiet - Don't print any messages or errors.

@@ -428,25 +705,25 @@ * @param {Number=} options.resolutionXAxis - Specifies the X resolution, in pixels per inch of

* pages, height for portrait pages) to fit in scale-to pixels. The size of the short side will
* be determined by the aspect ratio of the page (PNG/JPEG/TIFF only).
* be determined by the aspect ratio of the page.
* @param {Number=} options.scalePageToXAxis - Scales each page horizontally to fit in scale-to-x
* pixels. If scale-to-y is set to -1, the vertical size will determined by the aspect ratio of
* the page (PNG/JPEG/TIFF only).
* the page.
* @param {Number=} options.scalePageToYAxis - Scales each page vertically to fit in scale-to-y
* pixels. If scale-to-x is set to -1, the horizontal size will determined by the aspect ratio of
* the page (PNG/JPEG/TIFF only).
* the page.
* @param {Boolean=} options.singleFile - Writes only the first page and does not add digits.
* @param {Boolean=} options.svgFile - Generate a SVG (Scalable Vector Graphics) file.
* @param {String=} options.thinLineMode - Specifies the thin line mode. This defaults to "none".
* Options are: none; solid; shape.
* @param {String=} options.tiffCompression - Set TIFF compression to one of "none", "packbits",
* "jpeg", "lzw", or "deflate".
* @param {Boolean=} options.tiffFile - Generates a TIFF file(s).
* @param {Boolean=} options.transparentPageColor - Use a transparent page color
* instead of white (PNG and TIFF only).
* @param {Boolean=} options.tiffFile - Generates a TIFF file instead a PPM file.
* @param {String=} options.userPassword - Specify the user password for the PDF file.
* @param {String} file - Filepath of the PDF file to read.
* @param {String=} outputFile - Filepath of the file to output the results to.
* @param {String} outputPath - Filepath to output the results to.
* @returns {Promise}
*/
pdfToCairo(options, file, outputFile) {
pdfToPpm(options, file, outputPath) {
return new Promise((resolve, reject) => {
const acceptedOptions = {
antialias: { arg: '-antialias', type: 'string' },
antialiasFonts: { arg: '-aa', type: 'string' },
antialiasVectors: { arg: '-aaVector', type: 'string' },
cropBox: { arg: '-cropbox', type: 'boolean' },

@@ -458,27 +735,13 @@ cropHeight: { arg: '-H', type: 'number' },

cropYAxis: { arg: '-y', type: 'number' },
duplex: { arg: '-duplex', type: 'boolean' },
epsFile: { arg: '-eps', type: 'boolean' },
evenPagesOnly: { arg: '-e', type: 'boolean' },
fillPage: { arg: '-expand', type: 'boolean' },
firstPageToConvert: { arg: '-f', type: 'number' },
freetype: { arg: '-freetype', type: 'string' },
grayscaleFile: { arg: '-gray', type: 'boolean' },
iccFile: { arg: '-icc', type: 'string' },
jpegFile: { arg: '-jpeg', type: 'boolean' },
lastPageToConvert: { arg: '-l', type: 'number' },
monochromeFile: { arg: '-mono', type: 'boolean' },
noCenter: { arg: '-nocenter', type: 'boolean' },
noCrop: { arg: '-nocrop', type: 'boolean' },
noShrink: { arg: '-noshrink', type: 'boolean' },
oddPagesOnly: { arg: '-o', type: 'boolean' },
originalPageSizes: { arg: '-origpagesizes', type: 'boolean' },
ownerPassword: { arg: '-opw', type: 'string' },
paperHeight: { arg: '-paperh', type: 'number' },
paperSize: { arg: '-paper', type: 'string' },
paperWidth: { arg: '-paperw', type: 'number' },
pdfFile: { arg: '-pdf', type: 'boolean' },
pngFile: { arg: '-png', type: 'boolean' },
printVersionInfo: { arg: '-v', type: 'boolean' },
psFile: { arg: '-ps', type: 'boolean' },
psLevel2: { arg: '-level2', type: 'boolean' },
psLevel3: { arg: '-level3', type: 'boolean' },
quiet: { arg: '-q', type: 'boolean' },

@@ -492,6 +755,5 @@ resolutionXAxis: { arg: '-rx', type: 'number' },

singleFile: { arg: '-singlefile', type: 'boolean' },
svgFile: { arg: '-svg', type: 'boolean' },
thinLineMode: { arg: '-thinlinemode', type: 'string' },
tiffCompression: { arg: '-tiffcompression', type: 'string' },
tiffFile: { arg: '-tiff', type: 'boolean' },
transparentPageColor: { arg: '-transp', type: 'boolean' },
userPassword: { arg: '-upw', type: 'string' }

@@ -514,8 +776,171 @@ };

args.push(file);
if (outputFile) {
args.push(outputFile);
args.push(outputPath);
execFile(
path.join(this.popplerPath, 'pdftoppm'),
args,
(err, stdout) => {
if (err) {
reject(err);
} else {
resolve(stdout);
}
}
);
});
}
/**
* @author Frazer Smith
* @description Converts PDF to PostScript (PS).
*
* @param {Object=} options
* @param {String=} options.antialias - Enable anti-aliasing on rasterization, accepts "yes" or "no".
* @param {Boolean=} options.binary - Write binary data in Level 1 PostScript. By default,
* pdftops writes hex-encoded data in Level 1 PostScript. Binary data is non-standard in Level 1
* PostScript but reduces the file size and can be useful when Level 1 PostScript is required
* only for its restricted use of PostScript operators.
* @param {Boolean=} options.duplex - Set the Duplex pagedevice entry in the PostScript file.
* This tells duplex-capable printers to enable duplexing.
* @param {Boolean=} options.epsFile - Generate an EPS file. An EPS file contains a single image,
* so if you use this option with a multi-page PDF file, you must use 'firstPageToConvert' and
* 'lastPageToConvert' to specify a single page.
* The page size options (originalPageSizes, paperSize, paperWidth, paperHeight) can not be used
* with this option.
* @param {Boolean=} options.fillPage - Expand PDF pages smaller than the paper to fill the
* paper. By default, these pages are not scaled.
* @param {Number=} options.firstPageToConvert - Specifies the first page to convert.
* @param {Number=} options.form - Generate a PostScript form which can be imported by software
* that understands forms.
* A form contains a single page, so if you use this option with a multi-page PDF file,
* you must use 'firstPageToConvert' and 'lastPageToConvert to specify a single page.
* The 'level1' option cannot be used with -form.
* No more than one of the mode options ('epsFile', 'form') may be given.
* @param {Number=} options.lastPageToConvert - Specifies the last page to convert.
* @param {Boolean=} options.level1 - Generate Level 1 PostScript. The resulting PostScript
* files will be significantly larger (if they contain images), but will print on Level 1 printers.
* This also converts all images to black and white.
* @param {Boolean=} options.level1Sep - Generate Level 1 separable PostScript.
* All colors are converted to CMYK. Images are written with separate stream data for the four components.
* @param {Boolean=} options.level2 - Generate Level 2 PostScript.
* Level 2 supports color images and image compression. This is the default setting.
* @param {Boolean=} options.level2Sep - Generate Level 2 separable PostScript. All colors are
* converted to CMYK. The PostScript separation convention operators are used to handle custom (spot) colors.
* @param {Boolean=} options.level3 - Generate Level 3 PostScript.
* This enables all Level 2 featuresplus CID font embedding.
* @param {Boolean=} options.level3Sep - Generate Level 3 separable PostScript.
* The separation handling is the same as for 'level2Sep'.
* @param {Boolean=} options.noEmbedCIDFonts - By default, any CID PostScript fonts which are
* embedded in the PDF file are copied into the PostScript file. This option disables that embedding.
* No attempt is made to substitute for non-embedded CID PostScript fonts.
* @param {Boolean=} options.noEmbedCIDTrueTypeFonts - By default, any CID TrueType fonts which are
* embedded in the PDF file are copied into the PostScript file. This option disables that embedding.
* No attempt is made to substitute for non-embedded CID TrueType fonts.
* @param {Boolean=} options.noEmbedTrueTypeFonts - By default, any TrueType fonts which are embedded
* in the PDF file are copied into the PostScript file. This option causes pdftops to substitute base fonts instead.
* Embedded fonts make PostScript files larger, but may be necessary for readable output.
* Also, some PostScript interpreters do not have TrueType rasterizers.
* @param {Boolean=} options.noEmbedType1Fonts - By default, any Type 1 fonts which are embedded in the PDF file
* are copied into the PostScript file. This option causes pdftops to substitute base fonts instead.
* Embedded fonts make PostScript files larger, but may be necessary for readable output.
* @param {Boolean=} options.noCenter - By default, PDF pages smaller than the paper
* (after any scaling) are centered on the paper. This option causes them to be aligned to
* the lower-left corner of the paper instead.
* @param {Boolean=} options.noCrop - By default, printing output is cropped to the CropBox
* specified in the PDF file. This option disables cropping.
* @param {Boolean=} options.noShrink - Don't scale PDF pages which are larger than the paper.
* By default, pages larger than the paper are shrunk to fit.
* @param {Boolean=} options.opi - Generate OPI comments for all images and forms which have OPI information.
* @param {Boolean=} options.optimizecolorspace - By default, bitmap images in the PDF pass through to the
* output PostScript in their original color space, which produces predictable results.
* This option converts RGB and CMYK images into Gray images if every pixel of the image has equal components.
* This can fix problems when doing color separations of PDFs that contain embedded black and
* white images encoded as RGB.
* @param {Boolean=} options.originalPageSizes - Set the paper size of each page to match
* the size specified in the PDF file.
* @param {Boolean=} options.overprint - Enable overprinting.
* @param {String=} options.ownerPassword - Owner password (for encrypted files).
* @param {Number=} options.paperHeight - Set the paper height, in points.
* @param {String=} options.paperSize - Set the paper size to one of "letter", "legal", "A4",
* or "A3". This can also be set to "match", which will set the paper size
* of each page to match the size specified in the PDF file. If none of the paperSize,
* paperWidth, or paperHeight options are specified the default is to match the paper size.
* @param {Number=} options.paperWidth - Set the paper width, in points.
* @param {Boolean=} options.passfonts - By default, references to non-embedded 8-bit fonts
* in the PDF file are substituted with the closest "Helvetica", "Times-Roman", or "Courier" font.
* This option passes references to non-embedded fonts through to the PostScript file.
* @param {Boolean=} options.preload - Preload images and forms.
* @param {Boolean=} options.printVersionInfo - Print copyright and version information.
* @param {Boolean=} options.quiet - Don't print any messages or errors.
* @param {Number=} options.resolutionXYAxis - Specifies the X and Y resolution, in pixels per
* inch of image files (or rasterized regions in vector output). The default is 300 PPI.
* @param {String=} options.userPassword - User password (for encrypted files).
* @param {String} file - Filepath of the PDF file to read.
* @param {String} outputFile - Filepath of the file to output the results to.
* @returns {Promise}
*/
pdfToPs(options, file, outputFile) {
return new Promise((resolve, reject) => {
const acceptedOptions = {
antialias: { arg: '-aaRaster', type: 'string' },
binary: { arg: '-binary', type: 'boolean' },
duplex: { arg: '-duplex', type: 'boolean' },
epsFile: { arg: '-eps', type: 'boolean' },
fillPage: { arg: '-expand', type: 'boolean' },
firstPageToConvert: { arg: '-f', type: 'number' },
form: { arg: '-form', type: 'boolean' },
lastPageToConvert: { arg: '-l', type: 'number' },
level1: { arg: '-level1', type: 'boolean' },
level1Sep: { arg: '-level1sep', type: 'boolean' },
level2: { arg: '-level2', type: 'boolean' },
level2Sep: { arg: '-level2sep', type: 'boolean' },
level3: { arg: '-level3', type: 'boolean' },
level3Sep: { arg: '-level3sep', type: 'boolean' },
noEmbedCIDFonts: { arg: '-noembcidps', type: 'boolean' },
noEmbedCIDTrueTypeFonts: {
arg: '-noembcidtt',
type: 'boolean'
},
noEmbedTrueTypeFonts: { arg: '-noembtt', type: 'boolean' },
noEmbedType1Fonts: { arg: '-noembt1', type: 'boolean' },
noCenter: { arg: '-nocenter', type: 'boolean' },
noCrop: { arg: '-nocrop', type: 'boolean' },
noShrink: { arg: '-noshrink', type: 'boolean' },
opi: { arg: '-opi', type: 'boolean' },
optimizecolorspace: {
arg: '-optimizecolorspace',
type: 'boolean'
},
originalPageSizes: { arg: '-origpagesizes', type: 'boolean' },
overprint: { arg: '-overprint', type: 'boolean' },
ownerPassword: { arg: '-opw', type: 'string' },
paperHeight: { arg: '-paperh', type: 'number' },
paperSize: { arg: '-paper', type: 'string' },
paperWidth: { arg: '-paperw', type: 'number' },
passfonts: { arg: '-passfonts', type: 'boolean' },
preload: { arg: '-preload', type: 'boolean' },
printVersionInfo: { arg: '-v', type: 'boolean' },
quiet: { arg: '-q', type: 'boolean' },
resolutionXYAxis: { arg: '-r', type: 'number' },
userPassword: { arg: '-upw', type: 'string' }
};
// Build array of args based on options passed
const args = [];
/**
* Check each option provided is valid and of the correct type,
* before adding it to argument list.
*/
if (options) {
parseOptions(options, acceptedOptions, args).catch((err) => {
reject(err);
});
}
args.push(file);
args.push(outputFile);
execFile(
path.join(this.popplerPath, 'pdftocairo'),
path.join(this.popplerPath, 'pdftops'),
args,

@@ -551,3 +976,3 @@ (err, stdout) => {

* @param {String=} options.eolConvention - Sets the end-of-line convention to use for
* text output: unix | dos | mac.
* text output: unix; dos; mac.
* @param {Number=} options.firstPageToConvert - Specifies the first page to convert.

@@ -561,4 +986,5 @@ * @param {Number=} options.fixedWidthLayout - Assume fixed-pitch (or tabular) text, with the

* @param {Boolean=} options.maintainLayout - Maintain (as best as possible) the original physical
* layout of the text. The default is to ´undo' physical layout (columns, hyphenation, etc.) and
* layout of the text. The default is to undo physical layout (columns, hyphenation, etc.) and
* output the text in reading order.
* @param {Boolean=} options.noDiagonalText - Discard diagonal text.
* @param {Boolean=} options.noPageBreaks - Don't insert page breaks (form feed characters)

@@ -597,2 +1023,3 @@ * between pages.

maintainLayout: { arg: '-layout', type: 'boolean' },
noDiagonalText: { arg: '-nodiag', type: 'boolean' },
noPageBreaks: { arg: '-nopgbrk', type: 'boolean' },

@@ -599,0 +1026,0 @@ outputEncoding: { arg: '-enc', type: 'string' },

@@ -13,3 +13,18 @@ const fs = require('fs');

}
if (fs.existsSync(`${testDirectory}pdf_1.3_NHS_Constitution.ps`)) {
fs.unlinkSync(`${testDirectory}pdf_1.3_NHS_Constitution.ps`);
}
if (fs.existsSync(`${testDirectory}pdf_1.3_NHS_Constitution-01.ppm`)) {
fs.unlinkSync(`${testDirectory}pdf_1.3_NHS_Constitution-01.ppm`);
}
if (
fs.existsSync(
`${testDirectory}pdf_1.3_NHS_Constitution_attached.pdf`
)
) {
fs.unlinkSync(
`${testDirectory}pdf_1.3_NHS_Constitution_attached.pdf`
);
}
if (
fs.existsSync(`${testDirectory}pdf_1.3_NHS_Constitution_ind.html`)

@@ -72,3 +87,3 @@ ) {

'win32',
'poppler-0.68.0',
'poppler-0.84.0',
'bin'

@@ -91,2 +106,61 @@ );

describe('pdfAttach function', () => {
afterAll(async () => {
await clean();
});
test('Should attach file to PDF file', async () => {
const poppler = new Poppler();
const attachmentFile = `${testDirectory}test.txt`;
const outputFile = `${testDirectory}pdf_1.3_NHS_Constitution_attached.pdf`;
await poppler
.pdfAttach(undefined, file, attachmentFile, outputFile)
.then((res) => {
expect(typeof res).toBe('string');
expect(
fs.existsSync(
`${testDirectory}pdf_1.3_NHS_Constitution_attached.pdf`
)
).toBe(true);
});
});
test('Should return an Error object if file passed not PDF format', async () => {
const poppler = new Poppler();
const testTxtFile = `${testDirectory}test.txt`;
expect.assertions(1);
await poppler.pdfAttach(undefined, testTxtFile).catch((err) => {
expect(err.message.substring(0, 15)).toBe('Command failed:');
});
});
test('Should return an Error object if invalid value types provided for an option are passed to function', async () => {
const poppler = new Poppler();
const options = {
replace: 'test'
};
expect.assertions(1);
await poppler.pdfAttach(options, file).catch((err) => {
expect(err.message).toEqual(
"Invalid value type provided for option 'replace', expected boolean but recieved string"
);
});
});
test('Should return an Error object if invalid option is passed to function', async () => {
const poppler = new Poppler();
const options = {
wordFile: 'test'
};
expect.assertions(1);
await poppler.pdfAttach(options, file).catch((err) => {
expect(err.message).toEqual("Invalid option provided 'wordFile'");
});
});
});
describe('pdfDetach function', () => {

@@ -187,2 +261,44 @@ afterAll(async () => {

describe('pdfInfo function', () => {
afterAll(async () => {
await clean();
});
test('Should return an Error object if file passed not PDF format', async () => {
const poppler = new Poppler();
const testTxtFile = `${testDirectory}test.txt`;
expect.assertions(1);
await poppler.pdfInfo(undefined, testTxtFile).catch((err) => {
expect(err.message.substring(0, 15)).toBe('Command failed:');
});
});
test('Should return an Error object if invalid value types provided for an option are passed to function', async () => {
const poppler = new Poppler();
const options = {
firstPageToConvert: 'test'
};
expect.assertions(1);
await poppler.pdfInfo(options, file).catch((err) => {
expect(err.message).toEqual(
"Invalid value type provided for option 'firstPageToConvert', expected number but recieved string"
);
});
});
test('Should return an Error object if invalid option is passed to function', async () => {
const poppler = new Poppler();
const options = {
wordFile: 'test'
};
expect.assertions(1);
await poppler.pdfInfo(options, file).catch((err) => {
expect(err.message).toEqual("Invalid option provided 'wordFile'");
});
});
});
describe('pdfSeparate function', () => {

@@ -421,2 +537,156 @@ afterAll(async () => {

describe('pdfToPpm function', () => {
afterAll(async () => {
await clean();
});
test('Should accept options and only process 1 page of PDF file', async () => {
const poppler = new Poppler();
const options = {
firstPageToConvert: 1,
lastPageToConvert: 1
};
await poppler
.pdfToPpm(options, file, `${testDirectory}pdf_1.3_NHS_Constitution`)
.then((res) => {
expect(typeof res).toBe('string');
expect(
fs.existsSync(
`${testDirectory}pdf_1.3_NHS_Constitution-01.ppm`
)
).toBe(true);
});
});
test('Should return an Error object if file passed not PDF format', async () => {
const poppler = new Poppler();
const testTxtFile = `${testDirectory}test.txt`;
expect.assertions(1);
await poppler.pdfToPpm(undefined, testTxtFile).catch((err) => {
expect(err.message.substring(0, 15)).toBe('Command failed:');
});
});
test('Should return an Error object if PDF file missing', async () => {
const poppler = new Poppler();
expect.assertions(1);
await poppler.pdfToPpm(undefined, undefined).catch((err) => {
expect(err.message.substring(0, 15)).toBe('Command failed:');
});
});
test('Should return an Error object if invalid value types provided for an option are passed to function', async () => {
const poppler = new Poppler();
const options = {
firstPageToConvert: 'test',
lastPageToConvert: 'test'
};
expect.assertions(1);
await poppler.pdfToPpm(options, undefined).catch((err) => {
expect(err.message).toEqual(
"Invalid value type provided for option 'firstPageToConvert', expected number but recieved string"
);
});
});
test('Should return an Error object if invalid option is passed to function', async () => {
const poppler = new Poppler();
const options = {
middlePageToConvert: 'test'
};
expect.assertions(1);
await poppler.pdfToPpm(options, undefined).catch((err) => {
expect(err.message).toEqual(
"Invalid option provided 'middlePageToConvert'"
);
});
});
});
describe('pdfToPs function', () => {
afterAll(async () => {
await clean();
});
test('Should convert PDF file to PS file', async () => {
const poppler = new Poppler();
const outputFile = `${testDirectory}pdf_1.3_NHS_Constitution.ps`;
await poppler.pdfToPs(undefined, file, outputFile).then((res) => {
expect(typeof res).toBe('string');
expect(
fs.existsSync(`${testDirectory}pdf_1.3_NHS_Constitution.ps`)
).toBe(true);
});
});
test('Should accept options and only process 2 pages of PDF file', async () => {
const poppler = new Poppler();
const options = {
firstPageToConvert: 1,
lastPageToConvert: 2
};
await poppler.pdfToPs(options, file).then((res) => {
expect(typeof res).toBe('string');
expect(
fs.existsSync(`${testDirectory}pdf_1.3_NHS_Constitution.ps`)
).toBe(true);
});
});
test('Should return an Error object if file passed not PDF format', async () => {
const poppler = new Poppler();
const testTxtFile = `${testDirectory}test.txt`;
expect.assertions(1);
await poppler.pdfToPs(undefined, testTxtFile).catch((err) => {
expect(err.message.substring(0, 15)).toBe('Command failed:');
});
});
test('Should return an Error object if PDF file missing', async () => {
const poppler = new Poppler();
expect.assertions(1);
await poppler.pdfToPs(undefined, undefined).catch((err) => {
expect(err.message.substring(0, 15)).toBe('Command failed:');
});
});
test('Should return an Error object if invalid value types provided for an option are passed to function', async () => {
const poppler = new Poppler();
const options = {
firstPageToConvert: 'test',
lastPageToConvert: 'test'
};
expect.assertions(1);
await poppler.pdfToPs(options, file).catch((err) => {
expect(err.message).toEqual(
"Invalid value type provided for option 'firstPageToConvert', expected number but recieved string"
);
});
});
test('Should return an Error object if invalid option is passed to function', async () => {
const poppler = new Poppler();
const options = {
middlePageToConvert: 'test'
};
expect.assertions(1);
await poppler.pdfToPs(options, file).catch((err) => {
expect(err.message).toEqual(
"Invalid option provided 'middlePageToConvert'"
);
});
});
});
describe('pdfToText function', () => {

@@ -423,0 +693,0 @@ afterAll(async () => {

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

test
I am a test file!

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

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