From 779e320fb7e9c1a0ab195903ecde9245c159b46b Mon Sep 17 00:00:00 2001 From: Victor Soares Date: Thu, 25 May 2023 09:04:40 -0300 Subject: [PATCH] :sparkles: feature: add gsModulePath option --- package.json | 2 +- src/compress.ts | 20 ++++++++++++++------ src/types.ts | 1 + 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 65b6b4b..1c02e72 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "compress-pdf", - "version": "0.3.3", + "version": "0.3.4", "templateVersion": "1.3.0", "description": "An compress pdf library using ghostscript", "main": "dist/index.js", diff --git a/src/compress.ts b/src/compress.ts index 42a8264..f2ed135 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -14,14 +14,18 @@ const defaultOptions: Required = { compatibilityLevel: 1.4, resolution: 'ebook', imageQuality: 100, + gsModulePath: '', binPath: getBinPath(os.platform()), }; async function compress(file: string | Buffer, options?: Options) { - const { resolution, imageQuality, compatibilityLevel, binPath } = defaults( - options, - defaultOptions - ); + const { + resolution, + imageQuality, + compatibilityLevel, + binPath, + gsModulePath, + } = defaults(options, defaultOptions); const output = path.resolve(os.tmpdir(), Date.now().toString()); const gsModule = getGSModulePath(binPath, os.platform()); @@ -30,13 +34,17 @@ async function compress(file: string | Buffer, options?: Options) { let tempFile: string | undefined; if (typeof file === 'string') { - command = `${gsModule} -q -dNOPAUSE -dBATCH -dSAFER -dSimulateOverprint=true -sDEVICE=pdfwrite -dCompatibilityLevel=${compatibilityLevel} -dPDFSETTINGS=/${resolution} -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=${imageQuality} -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=${imageQuality} -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=${imageQuality} -sOutputFile=${output} ${file}`; + command = `${ + gsModulePath || gsModule + } -q -dNOPAUSE -dBATCH -dSAFER -dSimulateOverprint=true -sDEVICE=pdfwrite -dCompatibilityLevel=${compatibilityLevel} -dPDFSETTINGS=/${resolution} -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=${imageQuality} -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=${imageQuality} -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=${imageQuality} -sOutputFile=${output} ${file}`; } else { tempFile = path.resolve(os.tmpdir(), (Date.now() * 2).toString()); await fs.promises.writeFile(tempFile, file); - command = `${gsModule} -q -dNOPAUSE -dBATCH -dSAFER -dSimulateOverprint=true -sDEVICE=pdfwrite -dCompatibilityLevel=${compatibilityLevel} -dPDFSETTINGS=/${resolution} -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=${imageQuality} -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=${imageQuality} -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=${imageQuality} -sOutputFile=${output} ${tempFile}`; + command = `${ + gsModulePath || gsModule + } -q -dNOPAUSE -dBATCH -dSAFER -dSimulateOverprint=true -sDEVICE=pdfwrite -dCompatibilityLevel=${compatibilityLevel} -dPDFSETTINGS=/${resolution} -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=${imageQuality} -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=${imageQuality} -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=${imageQuality} -sOutputFile=${output} ${tempFile}`; } await exec(command); diff --git a/src/types.ts b/src/types.ts index a136ea2..4d5255b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,6 +6,7 @@ export type Resolution = | 'default'; export type Options = { + gsModulePath?: string; compatibilityLevel?: number; /** * Can be