Skip to content

Commit 20e2438

Browse files
author
Mohamed Aziz Khayati
committed
Merge branch 'gabolera-main'
2 parents 1f7aa52 + b7ff1bd commit 20e2438

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

dist/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ export declare function rotatePDF(pdfBuffer: Buffer, direction: '90' | '180' | '
1111
*/
1212
export declare function renderPDFPagesToPNG(pdfBuffer: Buffer, firstPage?: number, lastPage?: number, resolution?: number): Promise<Buffer[]>;
1313
export declare function isValidPDF(pdfBuffer: Buffer): Promise<boolean>;
14+
/**
15+
* This function try, reduce size of your PDF not destroying quality
16+
* @param pdfBuffer Buffer
17+
* @returns Buffer
18+
*/
19+
export declare function compressPDF(pdfBuffer: Buffer | string, encoding?: BufferEncoding): Promise<Buffer>;

dist/index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
33
return (mod && mod.__esModule) ? mod : { "default": mod };
44
};
55
Object.defineProperty(exports, "__esModule", { value: true });
6-
exports.isValidPDF = exports.renderPDFPagesToPNG = exports.rotatePDF = exports.extractPDFPages = exports.countPDFPages = exports.combinePDFs = void 0;
6+
exports.compressPDF = exports.isValidPDF = exports.renderPDFPagesToPNG = exports.rotatePDF = exports.extractPDFPages = exports.countPDFPages = exports.combinePDFs = void 0;
77
const child_process_1 = __importDefault(require("child_process"));
88
const fs_extra_1 = __importDefault(require("fs-extra"));
99
const tempy_1 = __importDefault(require("tempy"));
@@ -166,3 +166,26 @@ async function isValidPDF(pdfBuffer) {
166166
}
167167
}
168168
exports.isValidPDF = isValidPDF;
169+
/**
170+
* This function try, reduce size of your PDF not destroying quality
171+
* @param pdfBuffer Buffer
172+
* @returns Buffer
173+
*/
174+
async function compressPDF(pdfBuffer, encoding) {
175+
try {
176+
if (typeof pdfBuffer === 'string') {
177+
pdfBuffer = Buffer.from(pdfBuffer, encoding !== null && encoding !== void 0 ? encoding : 'base64');
178+
}
179+
const compressedPdf = await useTempFilesPDFInOut(pdfBuffer, async (input, output) => {
180+
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}`);
181+
});
182+
if (pdfBuffer.length < compressedPdf.length) {
183+
return pdfBuffer;
184+
}
185+
return compressedPdf;
186+
}
187+
catch (e) {
188+
throw new Error('Failed optimize PDF: ' + e.message);
189+
}
190+
}
191+
exports.compressPDF = compressPDF;

src/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,27 @@ export async function isValidPDF(pdfBuffer: Buffer): Promise<boolean> {
201201
return false;
202202
}
203203
}
204+
205+
/**
206+
* This function try, reduce size of your PDF not destroying quality
207+
* @param pdfBuffer Buffer
208+
* @returns Buffer
209+
*/
210+
export async function compressPDF(pdfBuffer: Buffer | string, encoding?: BufferEncoding): Promise<Buffer> {
211+
try {
212+
if(typeof pdfBuffer === 'string'){
213+
pdfBuffer = Buffer.from(pdfBuffer, encoding ?? 'base64')
214+
}
215+
const compressedPdf = await useTempFilesPDFInOut(pdfBuffer, async (input, output) => {
216+
await exec(
217+
`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}`,
218+
);
219+
});
220+
if (pdfBuffer.length < compressedPdf.length) {
221+
return pdfBuffer;
222+
}
223+
return compressedPdf;
224+
} catch (e: any) {
225+
throw new Error('Failed optimize PDF: ' + e.message);
226+
}
227+
}

test/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,15 @@ describe('isValidPDF', () => {
112112
expect(await gs.isValidPDF(Buffer.from([1, 2, 3]))).toBe(false);
113113
});
114114
});
115+
116+
117+
describe('compressPDF', () => {
118+
test('returns PDF reduce size send buffer file', async () => {
119+
const optimizedPDF = await gs.compressPDF(files['pdf3.pdf'])
120+
expect(optimizedPDF.length).toBeLessThanOrEqual(files['pdf3.pdf'].length);
121+
});
122+
test('returns PDF reduce size send string encoded file', async () => {
123+
const optimizedPDF = await gs.compressPDF(files['pdf3.pdf'].toString('base64'))
124+
expect(optimizedPDF.length).toBeLessThanOrEqual(files['pdf3.pdf'].length);
125+
});
126+
})

0 commit comments

Comments
 (0)