Skip to content

Commit

Permalink
Merge pull request #32 from victorsoares96/feat/add-gsModulePath-option
Browse files Browse the repository at this point in the history
✨ feature: add gsModulePath option
  • Loading branch information
victorsoares96 committed May 25, 2023
2 parents 6a47815 + 779e320 commit 2dce5b7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "compress-pdf",
"version": "0.3.3",
"version": "0.3.4",
"templateVersion": "1.3.0",
"description": "An compress pdf library using ghostscript",
"main": "dist/index.js",
Expand Down
20 changes: 14 additions & 6 deletions src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ const defaultOptions: Required<Options> = {
compatibilityLevel: 1.4,
resolution: 'ebook',
imageQuality: 100,
gsModulePath: '',
binPath: getBinPath(os.platform()),
};

async function compress(file: string | Buffer, options?: Options) {
const { resolution, imageQuality, compatibilityLevel, binPath } = defaults(
options,
defaultOptions
);
const {
resolution,
imageQuality,
compatibilityLevel,
binPath,
gsModulePath,
} = defaults(options, defaultOptions);

const output = path.resolve(os.tmpdir(), Date.now().toString());
const gsModule = getGSModulePath(binPath, os.platform());
Expand All @@ -30,13 +34,17 @@ async function compress(file: string | Buffer, options?: Options) {
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}`;
command = `${
gsModulePath || 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}`;
command = `${
gsModulePath || 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);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type Resolution =
| 'default';

export type Options = {
gsModulePath?: string;
compatibilityLevel?: number;
/**
* Can be
Expand Down

0 comments on commit 2dce5b7

Please sign in to comment.