|
1 | | -# ghoulscript |
| 1 | +# @privyid/ghoulscript |
| 2 | +> Compress, merge, split, and render pages to image using Ghostscript |
| 3 | +
|
| 4 | +## Installation |
| 5 | + |
| 6 | +```bash |
| 7 | +yarn add @privyid/ghoulscript |
| 8 | +``` |
| 9 | + |
| 10 | +## How to Use |
| 11 | + |
| 12 | +### Using with Browser (Vite) |
| 13 | + |
| 14 | +```ts |
| 15 | +import { optimizePDF } from '@privyid/ghoulscript' |
| 16 | + |
| 17 | +const input = document.querySelector('input[type="file"]') |
| 18 | + |
| 19 | +input.addEventListener('change', async () => { |
| 20 | + if (input.files) { |
| 21 | + const file = input.files[0] |
| 22 | + const buffer = await file.arrayBuffer() |
| 23 | + const output = await optimizePDF(new Uint8Array(buffer)) |
| 24 | + |
| 25 | + window.open(URL.createObjectURL(new Blob([output], { type: 'application/pdf' })), '_blank') |
| 26 | + } |
| 27 | +}) |
| 28 | +``` |
| 29 | + |
| 30 | +### Using with NodeJS |
| 31 | +```ts |
| 32 | +import fs from 'node:fs/promises' |
| 33 | +import { resolve } from 'node:path' |
| 34 | +import { optimizePDF } from '@privyid/ghoulscript' |
| 35 | + |
| 36 | +const buffer = await fs.readFile(resolve(__dirname, './sample.pdf')) |
| 37 | +const output = await optimizePDF(buffer) |
| 38 | + |
| 39 | +await fs.writeFile(resolve(__dirname, './sample.compressed.pdf'), output) |
| 40 | +``` |
| 41 | + |
| 42 | +## Utilities |
| 43 | + |
| 44 | +### optimizePDF (file: Buffer, options?: CompressOptions) |
| 45 | + |
| 46 | +Compress and optimize PDF for Web Viewer. |
| 47 | + |
| 48 | +```ts |
| 49 | +import { optimizePDF } from '@privyid/ghoulscript' |
| 50 | + |
| 51 | +const buffer = await fs.readFile(resolve(__dirname, './sample.pdf')) |
| 52 | +const output = await optimizePDF(buffer, { password: '******' }) |
| 53 | + |
| 54 | +await fs.writeFile(resolve(__dirname, './sample.compressed.pdf'), output) |
| 55 | +``` |
| 56 | + |
| 57 | +#### CompressOptions |
| 58 | + |
| 59 | +| Options | Type | Default | Description | |
| 60 | +|---------------------------|:---------:|:--------:|------------------------------------------------------------------------------------| |
| 61 | +| `password` | `String` | - | Document protection password | |
| 62 | +| `pdfSettings` | `String` | `screen` | Preset setting, valid value is `screen`, `ebook`, `printer`, `prepress`, `default` | |
| 63 | +| `fastWebView` | `Boolean` | `true` | Enable Linearization | |
| 64 | +| `compatibilityLevel` | `String` | `1.4` | Compability version | |
| 65 | +| `colorConversionStrategy` | `String` | `RGB` | Color conversion strategy, valid value is `RGB`, `CMYK` | |
| 66 | +| `noTransparency` | `Boolean` | `true` | Remove transparency | |
| 67 | +| `keepPassword` | `Boolean` | `true` | Keep document password if document have a password protection | |
| 68 | +| `userPassword` | `String` | - | Set User Password to document | |
| 69 | +| `ownerPassword` | `String` | - | Set Owner Password to document | |
| 70 | + |
| 71 | +### combinePDF (files: Buffer[], options?: CompressOptions) |
| 72 | + |
| 73 | +Combine multiple PDF files into single PDF |
| 74 | + |
| 75 | +```ts |
| 76 | +import { combinePDF } from '@privyid/ghoulscript' |
| 77 | + |
| 78 | +const bufferA = await fs.readFile(resolve(__dirname, './sample-1.pdf')) |
| 79 | +const bufferB = await fs.readFile(resolve(__dirname, './sample-2.pdf')) |
| 80 | +const output = await combinePDF([bufferA, bufferB]) |
| 81 | + |
| 82 | +await fs.writeFile(resolve(__dirname, './sample.combine.pdf'), output) |
| 83 | +``` |
| 84 | + |
| 85 | +### splitPdf (file: Buffer, pageList: PageList[], options?: CompressOptions) |
| 86 | + |
| 87 | +Split single PDF into multiple files |
| 88 | + |
| 89 | +```ts |
| 90 | +import { splitPdf } from '@privyid/ghoulscript' |
| 91 | + |
| 92 | +const buffer = await fs.readFile(resolve(__dirname, './sample.pdf')) |
| 93 | +const outputs = await splitPdf(buffer, ['1-5', '5-12']) |
| 94 | + |
| 95 | +await fs.writeFile(resolve(__dirname, './sample.part1.pdf'), outputs[0]) |
| 96 | +await fs.writeFile(resolve(__dirname, './sample.part2.pdf'), outputs[1]) |
| 97 | +``` |
| 98 | + |
| 99 | +### addPassword (file: Buffer, userPassword: string, ownerPassword?: string) |
| 100 | + |
| 101 | +Set new User Password and Owner Password |
| 102 | + |
| 103 | +```ts |
| 104 | +import { addPassword } from '@privyid/ghoulscript' |
| 105 | + |
| 106 | +const buffer = await fs.readFile(resolve(__dirname, './sample.pdf')) |
| 107 | +const output = await addPassword(buffer, '123456', '112233') |
| 108 | + |
| 109 | +await fs.writeFile(resolve(__dirname, './sample.protected.pdf'), output) |
| 110 | +``` |
| 111 | + |
| 112 | +It equivalent with compressPDF's `userPassword` and `ownerPassword` options |
| 113 | + |
| 114 | +### removePassword (file: Buffer, oldPassword: string) |
| 115 | + |
| 116 | +Remove existing encrypted PDF |
| 117 | + |
| 118 | +```ts |
| 119 | +import { removePassword } from '@privyid/ghoulscript' |
| 120 | + |
| 121 | +const buffer = await fs.readFile(resolve(__dirname, './sample.protected.pdf')) |
| 122 | +const output = await removePassword(buffer, '123456') |
| 123 | + |
| 124 | +await fs.writeFile(resolve(__dirname, './sample.unprotected.pdf'), output) |
| 125 | +``` |
| 126 | + |
| 127 | +It equivalent with compressPDF's `keepPassword: false` |
| 128 | + |
| 129 | +### renderPageAsImage (file: Buffer, page: number = 1, options?: RenderOptions) |
| 130 | + |
| 131 | +Convert specific page to image |
| 132 | + |
| 133 | +```ts |
| 134 | +import { renderPageAsImage } from '@privyid/ghoulscript' |
| 135 | + |
| 136 | +const buffer = await fs.readFile(resolve(__dirname, './sample.pdf')) |
| 137 | +const output = await renderPageAsImage(buffer, 5, { format: 'jpg' }) |
| 138 | + |
| 139 | +await fs.writeFile(resolve(__dirname, './sample.jpg'), output) |
| 140 | +``` |
| 141 | + |
| 142 | +#### RenderOptions |
| 143 | + |
| 144 | +| Options | Type | Default | Description | |
| 145 | +|---------------------|:--------:|:-------:|----------------------------------------------| |
| 146 | +| `resolution` | `Number` | `96` | Render resolution | |
| 147 | +| `textAlphaBits` | `Number` | `4` | Text alpha bits, valid value is `1`-`4` | |
| 148 | +| `graphicsAlphaBits` | `Number` | `4` | Graphic alpha bits, valid value is `1`-`4` | |
| 149 | +| `format` | `String` | `jpg` | Render format, valid value is `jpg` or `png` | |
| 150 | + |
| 151 | +## getInfo (file: Buffer, options?: { password: string }) |
| 152 | + |
| 153 | +Extract pages info |
| 154 | + |
| 155 | +```ts |
| 156 | +import { getInfo } from '@privyid/ghoulscript' |
| 157 | + |
| 158 | +const buffer = await fs.readFile(resolve(__dirname, './sample.pdf')) |
| 159 | +const info = await getInfo(buffer) |
| 160 | + |
| 161 | +console.log(info) |
| 162 | +/* |
| 163 | +{ |
| 164 | + numPages: 5, |
| 165 | + pages: [ |
| 166 | + { |
| 167 | + page: 1, |
| 168 | + width: 612, |
| 169 | + height: 792, |
| 170 | + }, |
| 171 | + { |
| 172 | + page: 2, |
| 173 | + width: 612, |
| 174 | + height: 792, |
| 175 | + }, |
| 176 | + { |
| 177 | + page: 3, |
| 178 | + width: 612, |
| 179 | + height: 792, |
| 180 | + }, |
| 181 | + ] |
| 182 | +} |
| 183 | +*/ |
| 184 | +``` |
2 | 185 |
|
3 | 186 | ## License |
4 | 187 |
|
|
0 commit comments