Skip to content

Commit

Permalink
Merge pull request #26 from victorsoares96/feat/support-pdf-buffer-as…
Browse files Browse the repository at this point in the history
…-entry

✨ feature: support pdf buffer in entry file
  • Loading branch information
victorsoares96 committed Apr 8, 2023
2 parents 2d408bc + 89f92fa commit d22beef
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
4 changes: 2 additions & 2 deletions examples/basic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import fs from 'fs';
import compressPdf from '@/index';
import { compress } from '@/index';

/**
* Before run:
Expand All @@ -11,7 +11,7 @@ import compressPdf from '@/index';

(async () => {
const pdf = path.resolve(__dirname, 'A17_FlightPlan.pdf');
const buffer = await compressPdf(pdf);
const buffer = await compress(pdf);

const compressedPdf = path.resolve(__dirname, 'compressed_pdf.pdf');
await fs.promises.writeFile(compressedPdf, buffer);
Expand Down
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.2.2",
"version": "0.3.0",
"templateVersion": "1.3.0",
"description": "An compress pdf library using ghostscript",
"main": "dist/index.js",
Expand Down
25 changes: 18 additions & 7 deletions src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const defaultOptions: Required<Options> = {
binPath: getBinPath(os.platform()),
};

async function compress(file: string, options?: Options) {
async function compress(file: string | Buffer, options?: Options) {
const { resolution, imageQuality, compatibilityLevel, binPath } = defaults(
options,
defaultOptions
Expand All @@ -26,16 +26,27 @@ async function compress(file: string, options?: Options) {
const output = path.resolve(os.tmpdir(), Date.now().toString());
const gsModule = getGSModulePath(binPath, os.platform());

await exec(
`${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}`
);
let command: string;
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}`;
} 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}`;
}

await exec(command);

if (tempFile) await fs.unlinkSync(tempFile);

const readFile = await fs.promises.readFile(output);
const buffer = Buffer.from(readFile);

await fs.unlinkSync(output);
await fs.unlinkSync(readFile);

return buffer;
return readFile;
}

export default compress;
1 change: 0 additions & 1 deletion src/get-gs-module-path.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs';
import path from 'path';
import os from 'os';
import { GS_VERSION } from './fetch-binaries';

function getGSModulePath(binPath: string, platform: NodeJS.Platform) {
const arch = os.arch();
Expand Down
19 changes: 4 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import compress from '@/compress';
import getBinPath from './get-bin-path';
import getGSModulePath from './get-gs-module-path';
import fetchBinaries, { GS_VERSION, RELEASE_URL } from './fetch-binaries';
export { default as getGSModulePath } from './get-gs-module-path';
export { default as getBinPath } from './get-bin-path';
export { default as compress } from './compress';

export * from './fetch-binaries';
export * from './types';

export {
compress,
getBinPath,
getGSModulePath,
fetchBinaries,
GS_VERSION,
RELEASE_URL,
};

export default compress;

0 comments on commit d22beef

Please sign in to comment.