@nigelnguyen/2fcompresspdf
v0.1.4
Published
Pure JS PDF compressor CLI using PDF.js, @napi-rs/canvas, and pdf-lib. No Ghostscript required.
Maintainers
Readme
@nigelnguyen/2fcompresspdf
Pure JS PDF compressor CLI/library.
- No Ghostscript
- Uses
pdfjs-distto render pages - Uses
@napi-rs/canvasto rasterize pages in Node - Uses
pdf-libto write the compressed PDF - Cross-platform npm install, depending on
@napi-rs/canvasprebuilt support
Install
npm i -g @nigelnguyen/2fcompresspdfCLI
cpf input.pdf --dpi 300
cpf input.pdf --dpi 100 --output output.pdf
cpf input.pdf --dpi 100 --font-mode native
cpf input.pdf -d 50 -o small.pdfOutput default:
input-compressed-100dpi.pdfOptions
-d, --dpi <number> Target DPI, 10..300. Default: 72
-o, --output <path> Output PDF path
-q, --quality <number> JPEG quality, 0.1..0.95. Default: auto by DPI
--font-mode <mode> Font render mode: path|native. Default: path
--json Print JSON result
--quiet Hide progress
-h, --help Show help
-v, --version Show versionLibrary
const { compressPdfFile, compressPdfBuffer } = require('@nigelnguyen/2fcompresspdf');
await compressPdfFile('input.pdf', {
dpi: 100,
output: 'output.pdf',
});Important tradeoff
This compressor rasterizes each PDF page into a JPEG image, then builds a new PDF.
Pros:
- DPI actually affects output size
- no Ghostscript/system dependency
- works locally, no server/upload limit
Cons:
- text/vector become images
- output is not searchable/selectable
- large PDFs need enough local RAM/CPU
Publish
npm login
npm publish --access public