@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
4
4
} ;
5
5
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 ;
7
7
const child_process_1 = __importDefault ( require ( "child_process" ) ) ;
8
8
const fs_extra_1 = __importDefault ( require ( "fs-extra" ) ) ;
9
9
const tempy_1 = __importDefault ( require ( "tempy" ) ) ;
@@ -166,3 +166,26 @@ async function isValidPDF(pdfBuffer) {
166
166
}
167
167
}
168
168
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 ;
0 commit comments