From ff3cca36076d861a5c954941ee3c7632d63daaf4 Mon Sep 17 00:00:00 2001 From: Victor Soares Date: Thu, 6 Apr 2023 23:08:35 -0300 Subject: [PATCH 1/3] :memo: docs: update readme --- README.md | 105 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 97 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7094687..dcc69a3 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,105 @@ -# lib-template +# compress-pdf -## After all +This library provides compress your PDFs using ghostscript -Insert this secrets into your project repository. +## Requirements - +Download ghostscript binaries available in releases page inside this repository. Example name: `gs_10.01.linux/windows.zip` -```json -NPM_TOKEN= -CODECOV_TOKEN= +Then, extract and appoint: + +```sh +npm install gsx-pdf-optimize --global +``` + +### Code Usage + +```tsx +import * as React from 'react'; + +import { SafeAreaView, useWindowDimensions } from 'react-native'; +import { Reader, ReaderProvider } from '@epubjs-react-native/core'; +// import { useFileSystem } from '@epubjs-react-native/file-system'; // for Bare React Native project +// import { useFileSystem } from '@epubjs-react-native/expo-file-system'; // for Expo project + +export default function App() { + const { width, height } = useWindowDimensions(); + return ( + + + + + + ); +} +``` + +## CLI Usage + +``` +gsx-pdf-optimize input.pdf [output.pdf] [opts] + +Options: + --preset, -P one of: screen (default), printer, prepress, ebook + --dpi, -D image resampling resolution in DPI, default 300 + --quiet enable or disable logging (default quiet=true) + --command the Ghostscript command to use, default gsx +``` + +You can also override the command with the GSX_OPTIMIZE_COMMAND env var, e.g. if you want to set that in your bash profile. + +Examples: + +```sh +gsx-pdf-optimize input.pdf +gsx-pdf-optimize input.pdf output.pdf +gsx-pdf-optimize input.pdf --preset=ebook --dpi=96 +GSX_OPTIMIZE_COMMAND=gs gsx-pdf-optimize input.pdf output.pdf +``` + +## Raw Command + +Below is the raw command if you'd like to optimize it further yourself. Credit goes to @lkraider and [this gist](https://gist.github.com/lkraider/f0888da30bc352f9d167dfa4f4fc8213). + +```sh +gsx -sDEVICE=pdfwrite \ + -dPDFSETTINGS=/screen \ + -dNOPAUSE -dQUIET -dBATCH \ + -dCompatibilityLevel=1.5 \ + `# font settings` \ + -dSubsetFonts=true \ + -dCompressFonts=true \ + -dEmbedAllFonts=true \ + `# color format` \ + -sProcessColorModel=DeviceRGB \ + -sColorConversionStrategy=RGB \ + -sColorConversionStrategyForImages=RGB \ + -dConvertCMYKImagesToRGB=true \ + `# image resample` \ + -dDetectDuplicateImages=true \ + -dColorImageDownsampleType=/Bicubic \ + -dColorImageResolution=300 \ + -dGrayImageDownsampleType=/Bicubic \ + -dGrayImageResolution=300 \ + -dMonoImageDownsampleType=/Bicubic \ + -dMonoImageResolution=300 \ + -dDownsampleColorImages=true \ + `# preset overrides` \ + -dDoThumbnails=false \ + -dCreateJobTicket=false \ + -dPreserveEPSInfo=false \ + -dPreserveOPIComments=false \ + -dPreserveOverprintSettings=false \ + -dUCRandBGInfo=/Remove \ + -sOutputFile=output.pdf \ + input.pdf ``` ## License -lib-template is licensed under the [MIT License](http://www.opensource.org/licenses/mit-license.php). +MIT, see [LICENSE.md](http://github.com/mattdesl/gsx-pdf-optimize/blob/master/LICENSE.md) for details. From 287a0a5e37e0bb42124eb0c6d36809006dce1c0a Mon Sep 17 00:00:00 2001 From: Victor Soares Date: Fri, 7 Apr 2023 09:38:38 -0300 Subject: [PATCH 2/3] :memo: docs: update readme and delete pdf in temp folder after store buffer --- README.md | 112 ++++++++++++++---------------------------------- src/compress.ts | 2 + 2 files changed, 35 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index dcc69a3..ff1b326 100644 --- a/README.md +++ b/README.md @@ -2,104 +2,58 @@ This library provides compress your PDFs using ghostscript -## Requirements +## Installation -Download ghostscript binaries available in releases page inside this repository. Example name: `gs_10.01.linux/windows.zip` +Download ghostscript binaries available in releases page inside this repository. Example name: `gs_10.01.linux/windows.zip` or run: `npx compress-pdf --fetchBinaries [win32/linux]` to download binaries to inside **lib_folder/bin/gs** Then, extract and appoint: ```sh -npm install gsx-pdf-optimize --global +npm install compress-pdf +``` + +```sh +yarn add compress-pdf ``` ### Code Usage ```tsx -import * as React from 'react'; - -import { SafeAreaView, useWindowDimensions } from 'react-native'; -import { Reader, ReaderProvider } from '@epubjs-react-native/core'; -// import { useFileSystem } from '@epubjs-react-native/file-system'; // for Bare React Native project -// import { useFileSystem } from '@epubjs-react-native/expo-file-system'; // for Expo project - -export default function App() { - const { width, height } = useWindowDimensions(); - return ( - - - - - - ); -} +import path from 'path'; +import fs from 'fs'; +import compressPdf from 'compress-pdf'; + +/** + * After this run: + * `npx compress-pdf --fetchBinaries [win32/linux]` + * This will download properly binaries to your current os. + */ + +(async () => { + const pdf = path.resolve(__dirname, 'A17_FlightPlan.pdf'); + const buffer = await compressPdf(pdf); + + const compressedPdf = path.resolve(__dirname, 'compressed_pdf.pdf'); + await fs.promises.writeFile(compressedPdf, buffer); +})(); ``` ## CLI Usage ``` -gsx-pdf-optimize input.pdf [output.pdf] [opts] +npx compress-pdf --file [PDF_FILE] --output ./compressed.pdf Options: - --preset, -P one of: screen (default), printer, prepress, ebook - --dpi, -D image resampling resolution in DPI, default 300 - --quiet enable or disable logging (default quiet=true) - --command the Ghostscript command to use, default gsx -``` - -You can also override the command with the GSX_OPTIMIZE_COMMAND env var, e.g. if you want to set that in your bash profile. - -Examples: - -```sh -gsx-pdf-optimize input.pdf -gsx-pdf-optimize input.pdf output.pdf -gsx-pdf-optimize input.pdf --preset=ebook --dpi=96 -GSX_OPTIMIZE_COMMAND=gs gsx-pdf-optimize input.pdf output.pdf + --file [PDF_FILE] (REQUIRED) + --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 ``` -## Raw Command - -Below is the raw command if you'd like to optimize it further yourself. Credit goes to @lkraider and [this gist](https://gist.github.com/lkraider/f0888da30bc352f9d167dfa4f4fc8213). - -```sh -gsx -sDEVICE=pdfwrite \ - -dPDFSETTINGS=/screen \ - -dNOPAUSE -dQUIET -dBATCH \ - -dCompatibilityLevel=1.5 \ - `# font settings` \ - -dSubsetFonts=true \ - -dCompressFonts=true \ - -dEmbedAllFonts=true \ - `# color format` \ - -sProcessColorModel=DeviceRGB \ - -sColorConversionStrategy=RGB \ - -sColorConversionStrategyForImages=RGB \ - -dConvertCMYKImagesToRGB=true \ - `# image resample` \ - -dDetectDuplicateImages=true \ - -dColorImageDownsampleType=/Bicubic \ - -dColorImageResolution=300 \ - -dGrayImageDownsampleType=/Bicubic \ - -dGrayImageResolution=300 \ - -dMonoImageDownsampleType=/Bicubic \ - -dMonoImageResolution=300 \ - -dDownsampleColorImages=true \ - `# preset overrides` \ - -dDoThumbnails=false \ - -dCreateJobTicket=false \ - -dPreserveEPSInfo=false \ - -dPreserveOPIComments=false \ - -dPreserveOverprintSettings=false \ - -dUCRandBGInfo=/Remove \ - -sOutputFile=output.pdf \ - input.pdf -``` +**You can see examples in [examples folder](https://github.com/victorsoares96/compress-pdf/tree/master/examples)** ## License -MIT, see [LICENSE.md](http://github.com/mattdesl/gsx-pdf-optimize/blob/master/LICENSE.md) for details. +This project is under MIT license, see [LICENSE.md](https://github.com/victorsoares96/compress-pdf/blob/master/LICENSE) for details. diff --git a/src/compress.ts b/src/compress.ts index 3fe7981..5e4286d 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -33,6 +33,8 @@ async function compress(file: string, options?: Options) { const readFile = await fs.promises.readFile(output); const buffer = Buffer.from(readFile); + await fs.unlinkSync(output); + return buffer; } From e583631f8528b15da014a6eaabbe9db285c2cafa Mon Sep 17 00:00:00 2001 From: Victor Soares Date: Fri, 7 Apr 2023 09:40:10 -0300 Subject: [PATCH 3/3] :rocket: deploy: v0.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1c08c67..9151753 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "compress-pdf", - "version": "0.1.0", + "version": "0.2.0", "templateVersion": "1.3.0", "description": "An compress pdf library using ghostscript", "main": "dist/index.js",