From 9204a3eff7ed8144ce474838d0e7c04e5d3e3028 Mon Sep 17 00:00:00 2001 From: Victor Soares Date: Fri, 7 Apr 2023 22:10:24 -0300 Subject: [PATCH 1/2] :sparkles: feature: support pdf buffer in entry file --- package.json | 2 +- src/compress.ts | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 2cdb181..c4b4e13 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "compress-pdf", - "version": "0.2.2", + "version": "0.3.0", "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 5e4286d..21f9151 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -17,7 +17,7 @@ const defaultOptions: Required = { binPath: getBinPath(os.platform()), }; -async function compress(file: string, options?: Options) { +async function compress(file: string | Buffer, options?: Options) { const { resolution, imageQuality, compatibilityLevel, binPath } = defaults( options, defaultOptions @@ -26,16 +26,27 @@ async function compress(file: string, options?: Options) { const output = path.resolve(os.tmpdir(), Date.now().toString()); const gsModule = getGSModulePath(binPath, os.platform()); - await exec( - `${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}` - ); + let command: string; + 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}`; + } 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}`; + } + + await exec(command); + + if (tempFile) await fs.unlinkSync(tempFile); const readFile = await fs.promises.readFile(output); - const buffer = Buffer.from(readFile); - await fs.unlinkSync(output); + await fs.unlinkSync(readFile); - return buffer; + return readFile; } export default compress; From 89f92fa03b4a2087a75f0695d49265a37d8601e2 Mon Sep 17 00:00:00 2001 From: Victor Soares Date: Fri, 7 Apr 2023 22:24:24 -0300 Subject: [PATCH 2/2] :recycle: refactor: remove unused imports and fix root file --- examples/basic.ts | 4 ++-- src/get-gs-module-path.ts | 1 - src/index.ts | 19 ++++--------------- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/examples/basic.ts b/examples/basic.ts index c01bede..25c7247 100644 --- a/examples/basic.ts +++ b/examples/basic.ts @@ -1,6 +1,6 @@ import path from 'path'; import fs from 'fs'; -import compressPdf from '@/index'; +import { compress } from '@/index'; /** * Before run: @@ -11,7 +11,7 @@ import compressPdf from '@/index'; (async () => { const pdf = path.resolve(__dirname, 'A17_FlightPlan.pdf'); - const buffer = await compressPdf(pdf); + const buffer = await compress(pdf); const compressedPdf = path.resolve(__dirname, 'compressed_pdf.pdf'); await fs.promises.writeFile(compressedPdf, buffer); diff --git a/src/get-gs-module-path.ts b/src/get-gs-module-path.ts index f52b13d..1b28cc3 100644 --- a/src/get-gs-module-path.ts +++ b/src/get-gs-module-path.ts @@ -1,7 +1,6 @@ import fs from 'fs'; import path from 'path'; import os from 'os'; -import { GS_VERSION } from './fetch-binaries'; function getGSModulePath(binPath: string, platform: NodeJS.Platform) { const arch = os.arch(); diff --git a/src/index.ts b/src/index.ts index 0a08eb8..196e7ef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,17 +1,6 @@ -import compress from '@/compress'; -import getBinPath from './get-bin-path'; -import getGSModulePath from './get-gs-module-path'; -import fetchBinaries, { GS_VERSION, RELEASE_URL } from './fetch-binaries'; +export { default as getGSModulePath } from './get-gs-module-path'; +export { default as getBinPath } from './get-bin-path'; +export { default as compress } from './compress'; +export * from './fetch-binaries'; export * from './types'; - -export { - compress, - getBinPath, - getGSModulePath, - fetchBinaries, - GS_VERSION, - RELEASE_URL, -}; - -export default compress;