diff --git a/dist/index.d.ts b/dist/index.d.ts index 6936f80..740ef5a 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 | string, encoding?: BufferEncoding): Promise; diff --git a/dist/index.js b/dist/index.js index 88eb0cd..c43b861 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,26 @@ 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, 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}`); + }); + if (pdfBuffer.length < compressedPdf.length) { + return pdfBuffer; + } + return compressedPdf; + } + catch (e) { + throw new Error('Failed optimize PDF: ' + e.message); + } +} +exports.compressPDF = compressPDF; diff --git a/src/index.ts b/src/index.ts index 99df1d7..c167168 100644 --- a/src/index.ts +++ b/src/index.ts @@ -201,3 +201,27 @@ 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 | 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}`, + ); + }); + if (pdfBuffer.length < compressedPdf.length) { + return pdfBuffer; + } + return compressedPdf; + } catch (e: any) { + throw new Error('Failed optimize PDF: ' + e.message); + } +} diff --git a/test/index.test.ts b/test/index.test.ts index a581ebf..f7e9e7f 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -112,3 +112,15 @@ describe('isValidPDF', () => { expect(await gs.isValidPDF(Buffer.from([1, 2, 3]))).toBe(false); }); }); + + +describe('compressPDF', () => { + 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