From bb6e9db334887730b52448632a8a15d09614b020 Mon Sep 17 00:00:00 2001 From: Gabriel Andreazza Date: Wed, 22 Mar 2023 18:51:04 -0300 Subject: [PATCH 1/6] feat: :sparkles: create a `compressPDF` for reduce size of big pdfs using gs --- src/index.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/index.ts b/src/index.ts index 99df1d7..db0ff41 100644 --- a/src/index.ts +++ b/src/index.ts @@ -201,3 +201,24 @@ export async function isValidPDF(pdfBuffer: Buffer): Promise { return false; } } + +/** + * This function try, reduce size of your PDF not destroying quality + * @param pdfBuffer Buffer + * @returns Buffer + */ +export async function compressPDF(pdfBuffer: Buffer): Promise { + try { + const compressedPdf = await useTempFilesPDFInOut(pdfBuffer, async (input, output) => { + await exec( + `gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dColorImageDownsampleType=/Bicubic -dColorImageResolution=144 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=144 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=144 -sOutputFile=${output} ${input}`, + ); + }); + if (pdfBuffer.length < compressedPdf.length) { + return pdfBuffer; + } + return compressedPdf; + } catch (e: any) { + throw new Error('Failed optimize PDF: ' + e.message); + } +} From eb7c326cbb24ac1682e673df8c12266a077cc3a8 Mon Sep 17 00:00:00 2001 From: Gabriel Andreazza Date: Wed, 22 Mar 2023 18:52:14 -0300 Subject: [PATCH 2/6] test: :test_tube: add test for compressPDF function --- test/index.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/index.test.ts b/test/index.test.ts index a581ebf..b55b720 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -112,3 +112,11 @@ describe('isValidPDF', () => { expect(await gs.isValidPDF(Buffer.from([1, 2, 3]))).toBe(false); }); }); + + +describe('compressPDF', () => { + test('returns PDF reduce size', async () => { + const optimizedPDF = await gs.compressPDF(files['pdf3.pdf']) + expect(optimizedPDF.length).toBeLessThanOrEqual(files['pdf3.pdf'].length); + }); +}) \ No newline at end of file From 13c97439b7350295ebdd672fe01c7e3374316896 Mon Sep 17 00:00:00 2001 From: Gabriel Andreazza Date: Wed, 22 Mar 2023 18:53:55 -0300 Subject: [PATCH 3/6] build: :sparkles: build of new compressPDF function --- dist/index.d.ts | 6 ++++++ dist/index.js | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index 6936f80..0099ef1 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -11,3 +11,9 @@ export declare function rotatePDF(pdfBuffer: Buffer, direction: '90' | '180' | ' */ export declare function renderPDFPagesToPNG(pdfBuffer: Buffer, firstPage?: number, lastPage?: number, resolution?: number): Promise; export declare function isValidPDF(pdfBuffer: Buffer): Promise; +/** + * This function try, reduce size of your PDF not destroying quality + * @param pdfBuffer Buffer + * @returns Buffer + */ +export declare function compressPDF(pdfBuffer: Buffer): Promise; diff --git a/dist/index.js b/dist/index.js index 88eb0cd..6d49d06 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.isValidPDF = exports.renderPDFPagesToPNG = exports.rotatePDF = exports.extractPDFPages = exports.countPDFPages = exports.combinePDFs = void 0; +exports.compressPDF = exports.isValidPDF = exports.renderPDFPagesToPNG = exports.rotatePDF = exports.extractPDFPages = exports.countPDFPages = exports.combinePDFs = void 0; const child_process_1 = __importDefault(require("child_process")); const fs_extra_1 = __importDefault(require("fs-extra")); const tempy_1 = __importDefault(require("tempy")); @@ -166,3 +166,23 @@ async function isValidPDF(pdfBuffer) { } } exports.isValidPDF = isValidPDF; +/** + * This function try, reduce size of your PDF not destroying quality + * @param pdfBuffer Buffer + * @returns Buffer + */ +async function compressPDF(pdfBuffer) { + try { + const compressedPdf = await useTempFilesPDFInOut(pdfBuffer, async (input, output) => { + await exec(`gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dColorImageDownsampleType=/Bicubic -dColorImageResolution=144 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=144 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=144 -sOutputFile=${output} ${input}`); + }); + if (pdfBuffer.length < compressedPdf.length) { + return pdfBuffer; + } + return compressedPdf; + } + catch (e) { + throw new Error('Failed optimize PDF: ' + e.message); + } +} +exports.compressPDF = compressPDF; From f4b8b16af9ca4ce2b97b61a152351fdd51ba75f4 Mon Sep 17 00:00:00 2001 From: Gabriel Andreazza Date: Thu, 23 Mar 2023 09:23:21 -0300 Subject: [PATCH 4/6] feat: :sparkles: now accept files encoded `BufferEncoding` --- src/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index db0ff41..c167168 100644 --- a/src/index.ts +++ b/src/index.ts @@ -207,8 +207,11 @@ export async function isValidPDF(pdfBuffer: Buffer): Promise { * @param pdfBuffer Buffer * @returns Buffer */ -export async function compressPDF(pdfBuffer: Buffer): Promise { +export async function compressPDF(pdfBuffer: Buffer | string, encoding?: BufferEncoding): Promise { try { + if(typeof pdfBuffer === 'string'){ + pdfBuffer = Buffer.from(pdfBuffer, encoding ?? 'base64') + } const compressedPdf = await useTempFilesPDFInOut(pdfBuffer, async (input, output) => { await exec( `gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dColorImageDownsampleType=/Bicubic -dColorImageResolution=144 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=144 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=144 -sOutputFile=${output} ${input}`, From 040c78913b9dd8d827252cba18bf2d9a3fb16b4c Mon Sep 17 00:00:00 2001 From: Gabriel Andreazza Date: Thu, 23 Mar 2023 09:23:51 -0300 Subject: [PATCH 5/6] test: :test_tube: create a test for encoded file compressPDF --- test/index.test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/index.test.ts b/test/index.test.ts index b55b720..f7e9e7f 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -115,8 +115,12 @@ describe('isValidPDF', () => { describe('compressPDF', () => { - test('returns PDF reduce size', async () => { + test('returns PDF reduce size send buffer file', async () => { const optimizedPDF = await gs.compressPDF(files['pdf3.pdf']) expect(optimizedPDF.length).toBeLessThanOrEqual(files['pdf3.pdf'].length); }); + test('returns PDF reduce size send string encoded file', async () => { + const optimizedPDF = await gs.compressPDF(files['pdf3.pdf'].toString('base64')) + expect(optimizedPDF.length).toBeLessThanOrEqual(files['pdf3.pdf'].length); + }); }) \ No newline at end of file From b7ff1bdcc0917e2e5a35d8f8f0d5993c5850c279 Mon Sep 17 00:00:00 2001 From: Gabriel Andreazza Date: Thu, 23 Mar 2023 09:24:25 -0300 Subject: [PATCH 6/6] build: :rocket: up builded function `compressPDF` --- dist/index.d.ts | 2 +- dist/index.js | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index 0099ef1..740ef5a 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -16,4 +16,4 @@ export declare function isValidPDF(pdfBuffer: Buffer): Promise; * @param pdfBuffer Buffer * @returns Buffer */ -export declare function compressPDF(pdfBuffer: Buffer): Promise; +export declare function compressPDF(pdfBuffer: Buffer | string, encoding?: BufferEncoding): Promise; diff --git a/dist/index.js b/dist/index.js index 6d49d06..c43b861 100644 --- a/dist/index.js +++ b/dist/index.js @@ -171,8 +171,11 @@ exports.isValidPDF = isValidPDF; * @param pdfBuffer Buffer * @returns Buffer */ -async function compressPDF(pdfBuffer) { +async function compressPDF(pdfBuffer, encoding) { try { + if (typeof pdfBuffer === 'string') { + pdfBuffer = Buffer.from(pdfBuffer, encoding !== null && encoding !== void 0 ? encoding : 'base64'); + } const compressedPdf = await useTempFilesPDFInOut(pdfBuffer, async (input, output) => { await exec(`gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dColorImageDownsampleType=/Bicubic -dColorImageResolution=144 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=144 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=144 -sOutputFile=${output} ${input}`); });