Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feature: add support to darwin (macos) #37

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Darwin
.DS_Store

# Logs
logs
Expand Down
49 changes: 46 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ npm install compress-pdf
yarn add compress-pdf
```

### 🚨 Install binaries

**Ubuntu**

```sh
sudo apt-get install ghostscript -y
```

**MacOS**

```sh
brew install ghostscript
```

**Windows (Chocolatey)**

```sh
choco install ghostscript
```
or [download](https://ghostscript.com/releases/gsdnld.html) Ghostscript `.exe` installer

### Code Usage

```tsx
Expand All @@ -28,7 +49,7 @@ import { compress } from 'compress-pdf';
})();
```

## CLI Usage
### CLI Usage

```
npx compress-pdf --file [PDF_FILE] --output ./compressed.pdf
Expand All @@ -38,10 +59,32 @@ Options:
--output [COMPRESSED_PDF_FILE] (REQUIRED)
--resolution [ebook/printer/screen/prepress]
--compatibilityLevel [NUMBER] The compatibility pdf level
--binPath [DIRECTORY] The directory of ghostscript binaries. Default is lib_folder/bin/gs
--fetchBinaries [win32/linux] Download binaries to default binaries path
--gsModule [FILE PATH] The directory of ghostscript binaries. Ex: /usr/bin/gs
```

### Usage with Docker

```dockerfile
FROM node:18 AS build
WORKDIR /src
COPY package*.json ./
RUN npm pkg set scripts.scriptname="true" && npm i
COPY . .
RUN npm run build

FROM node:18
WORKDIR /app
RUN apt-get update \
&& apt-get install -y ghostscript
COPY package*.json ./
RUN npm pkg set scripts.scriptname="true" && npm i
COPY --from=build /src/build /app/build/
EXPOSE 8080
CMD [ "npm", "start" ]
```

**OBS:** This is just an example of how to use this lib in a docker image, note that you need to run apt-get to install ghostscript before doing anything

**You can see examples in [examples folder](https://github.com/victorsoares96/compress-pdf/tree/master/examples)**

## License
Expand Down
Binary file added examples/compressed_pdf.pdf
Binary file not shown.
23 changes: 0 additions & 23 deletions install.js

This file was deleted.

3 changes: 3 additions & 0 deletions instructions.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "compress-pdf",
"version": "0.3.7",
"version": "0.4.0",
"templateVersion": "1.3.0",
"description": "An compress pdf library using ghostscript",
"main": "dist/index.js",
Expand All @@ -11,20 +11,20 @@
"types": "dist/index.d.ts",
"scripts": {
"prepare": "husky install",
"postinstall": "node install",
"copy-json-files-to-dist": "copyfiles src/**/*.json install.js dist",
"postinstall": "node instructions",
"copy-json-files-to-dist": "copyfiles src/**/*.json instructions.js dist",
"style:format": "prettier \"*.{js,json,yml,yaml,md}\" \"src/**/*\" --write",
"lint": "eslint --color --ext .ts \"src/**/*.+(ts)\"",
"lint:fix": "eslint --color --ext .ts \"src/**/*.+(ts)\" --fix",
"test": "vitest",
"test:coverage": "vitest run --coverage",
"type-check": "tsc --pretty --noEmit --skipLibCheck --esModuleInterop --resolveJsonModule",
"package": "npm run build && npm pkg set scripts.postinstall='node dist/install' && npm pack && npm pkg set scripts.postinstall='node install'",
"package": "npm run build && npm pkg set scripts.postinstall='node dist/instructions' && npm pack && npm pkg set scripts.postinstall='node instructions'",
"build": "tsup",
"ci-postbuild": "npm pkg set scripts.postinstall='node dist/install'",
"release": "npm run build && npm pkg set scripts.postinstall='node dist/install' && npm publish && npm pkg set scripts.postinstall='node install'",
"release-beta": "npm run build && npm pkg set scripts.postinstall='node dist/install' && npm publish --tag beta && npm pkg set scripts.postinstall='node install'",
"release-local": "npm run build && npm pkg set scripts.postinstall='node dist/install' && npm link && npm pkg set scripts.postinstall='node install' && echo [FINISH]: Run \"npm link compress-pdf\" to link the package to your project"
"ci-postbuild": "npm pkg set scripts.postinstall='node dist/instructions'",
"release": "npm run build && npm pkg set scripts.postinstall='node dist/instructions' && npm publish && npm pkg set scripts.postinstall='node instructions'",
"release-beta": "npm run build && npm pkg set scripts.postinstall='node dist/instructions' && npm publish --tag beta && npm pkg set scripts.postinstall='node instructions'",
"release-local": "npm run build && npm pkg set scripts.postinstall='node dist/instructions' && npm link && npm pkg set scripts.postinstall='node instructions' && echo [FINISH]: Run \"npm link compress-pdf\" to link the package to your project"
},
"publishConfig": {
"access": "public"
Expand Down
7 changes: 4 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type CliOptions = {
'--output': string;
'--resolution'?: Resolution;
'--compatibilityLevel'?: string;
'--binPath'?: string;
'--gsModule'?: string;
'--fetchBinaries'?: NodeJS.Platform;
};

Expand All @@ -31,10 +31,11 @@ type CliOptions = {

if (
process.argv.slice(2).some((arg) => arg.includes('help')) ||
process.argv.slice(2).some((arg) => arg.includes('-h')) ||
process.argv.slice(2).length === 0
) {
return console.log(
'USE: npx compress-pdf\n--file [PDF_FILE]\n--output [COMPRESSED_PDF_FILE]\n--resolution [ebook/printer/screen/prepress]\n--compatibilityLevel [NUMBER] The compatibility pdf level\n--binPath [DIRECTORY] The directory of ghostscript binaries\n--fetchBinaries [win32/linux] Download binaries to default binaries path'
'USE: npx compress-pdf\n--file [PDF_FILE]\n--output [COMPRESSED_PDF_FILE]\n--resolution [ebook/printer/screen/prepress]\n--compatibilityLevel [NUMBER] The compatibility pdf level\n--gsModule [FILE PATH] The ghostscript binary path. Ex: /usr/local/bin/gs'
);
}

Expand All @@ -54,7 +55,7 @@ type CliOptions = {
compatibilityLevel: args['--compatibilityLevel']
? Number(args['--compatibilityLevel'])
: undefined,
binPath: args['--binPath'],
gsModule: args['--gsModule'],
});

return fs.writeFileSync(args['--output'], buffer);
Expand Down
16 changes: 5 additions & 11 deletions src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import os from 'os';
import childProcess from 'child_process';
import { defaults } from 'lodash';
import getBinPath from './get-bin-path';
import getGSModulePath from './get-gs-module-path';
import type { Options } from './types';

const exec = util.promisify(childProcess.exec);
Expand All @@ -14,21 +13,16 @@ const defaultOptions: Required<Options> = {
compatibilityLevel: 1.4,
resolution: 'ebook',
imageQuality: 100,
gsModulePath: '',
binPath: getBinPath(os.platform()),
gsModule: getBinPath(os.platform()),
};

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

const output = path.resolve(os.tmpdir(), Date.now().toString());
const gsModule = gsModulePath || getGSModulePath(binPath, os.platform());

let command: string;
let tempFile: string | undefined;
Expand Down
10 changes: 6 additions & 4 deletions src/get-bin-path.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import path from 'path';

function getBinPath(platform: NodeJS.Platform) {
if (platform === 'linux') {
return path.resolve(__dirname, '../bin/gs/10.01.1_linux');
return '/usr/bin/gs';
}

if (platform === 'win32') {
return path.resolve(__dirname, '../bin/gs/10.01.1_windows');
return 'gswin64c';
}

if (platform === 'darwin') {
return '/usr/local/bin/gs';
}

throw new Error(
Expand Down
3 changes: 1 addition & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export type Resolution =
| 'default';

export type Options = {
gsModulePath?: string;
compatibilityLevel?: number;
/**
* Can be
Expand Down Expand Up @@ -34,5 +33,5 @@ export type Options = {
*
* `You can download binaries in releases section inside any version of this repository.`
*/
binPath?: string;
gsModule?: string;
};
Loading