Skip to content

Commit 7ed5075

Browse files
committed
feat(ghoulscrupt): add option for additional args
1 parent 49ba8e0 commit 7ed5075

File tree

11 files changed

+1331
-1841
lines changed

11 files changed

+1331
-1841
lines changed

packages/ghostscript/build/post-build.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ async function createMeta () {
2020
}
2121

2222
await writeFile(resolve(_dirname, '../dist/meta.cjs'), `module.export = ${JSON.stringify(meta)}${EOL}`)
23-
2423
await writeFile(resolve(_dirname, '../dist/meta.mjs'), `export default ${JSON.stringify(meta)}${EOL}`)
25-
2624
await copyFile(resolve(_dirname, './meta.d.ts'), resolve(_dirname, '../dist/meta.d.ts'))
2725
}
2826

packages/ghoulscript/README.md

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,21 @@ await fs.writeFile(resolve(__dirname, './sample.compressed.pdf'), output)
5656

5757
#### CompressOptions
5858

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 Fast Web View (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 |
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 Fast Web View (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+
| `colorImageResolution` | `Number` | 300 | Color image resolution |
71+
| `grayImageResolution` | `Number` | 300 | Gray image resolution |
72+
| `monoImageResolution` | `Number` | 300 | Mono image resolution |
73+
| `args` | `String[]` | - | Additional arguments |
7074

7175
### combinePDF (files: Buffer[], options?: CompressOptions)
7276

@@ -141,12 +145,13 @@ await fs.writeFile(resolve(__dirname, './sample.jpg'), output)
141145

142146
#### RenderOptions
143147

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` |
148+
| Options | Type | Default | Description |
149+
|---------------------|:----------:|:-------:|----------------------------------------------|
150+
| `resolution` | `Number` | `96` | Render resolution |
151+
| `textAlphaBits` | `Number` | `4` | Text alpha bits, valid value is `1`-`4` |
152+
| `graphicsAlphaBits` | `Number` | `4` | Graphic alpha bits, valid value is `1`-`4` |
153+
| `format` | `String` | `jpg` | Render format, valid value is `jpg` or `png` |
154+
| `args` | `String[]` | - | Additional arguments |
150155

151156
## getInfo (file: Buffer, options?: { password: string })
152157

packages/ghoulscript/build.config.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/ghoulscript/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
],
1919
"scripts": {
2020
"prepare": "yarn build",
21-
"build": "unbuild",
21+
"build": "vite build",
2222
"test": "node --test"
2323
},
2424
"devDependencies": {
25-
"jiti": "1.21.0",
2625
"typescript": "5.4.5",
27-
"unbuild": "3.0.0-rc.6"
26+
"vite": "^5.3.3",
27+
"vite-plugin-dts": "^3.9.1",
28+
"vite-plugin-externalize-deps": "^0.8.0",
29+
"vite-plugin-node-polyfills": "^0.22.0"
2830
},
2931
"dependencies": {
3032
"@privyid/ghostscript": "workspace:^",

packages/ghoulscript/src/core.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ export interface CompressOptions {
6161
* createPDF({ pageList: ['1-3', '6-10'] })
6262
*/
6363
pageList?: PageList,
64+
/**
65+
* Color image resolution
66+
* @default 300
67+
*/
68+
colorImageResolution: number,
69+
/**
70+
* Gray image resolution
71+
* @default 300
72+
*/
73+
grayImageResolution: number,
74+
/**
75+
* Monochrome image resolution
76+
* @default 300
77+
*/
78+
monoImageResolution: number,
79+
/**
80+
* Additional arguments to Ghostscript
81+
*/
82+
args: string[],
6483
}
6584

6685
type InputFile = ArrayBufferView | Blob
@@ -81,9 +100,14 @@ async function createPDF (inputs: InputFile[], options: Partial<CompressOptions>
81100
fastWebView : true,
82101
noTransparency : true,
83102
keepPassword : true,
103+
colorImageResolution : 300,
104+
grayImageResolution : 300,
105+
monoImageResolution : 300,
106+
args : [],
84107
})
85108

86109
const args = [
110+
...opts.args,
87111
'-dQUIET',
88112
'-dNOPAUSE',
89113
'-dBATCH',
@@ -132,6 +156,9 @@ async function createPDF (inputs: InputFile[], options: Partial<CompressOptions>
132156
`-sColorConversionStrategy=${opts.colorConversionStrategy}`,
133157
`-dPDFSETTINGS=/${opts.pdfSettings}`,
134158
`-dFastWebView=${opts.fastWebView.toString()}`,
159+
`-dColorImageResolution=${opts.colorImageResolution}`,
160+
`-dGrayImageResolution=${opts.grayImageResolution}`,
161+
`-dMonoImageResolution=${opts.monoImageResolution}`,
135162
'-sOutputFile=./output',
136163
)
137164

@@ -227,6 +254,10 @@ export interface RenderOptions {
227254
* @default 'jpg'
228255
*/
229256
format: 'jpg' | 'png',
257+
/**
258+
* Additional arguments to Ghostscript
259+
*/
260+
args: string[],
230261
}
231262

232263
/**
@@ -243,10 +274,12 @@ export async function renderPageAsImage (input: InputFile, pageNumber: number =
243274
graphicsAlphaBits: 4,
244275
textAlphaBits : 4,
245276
resolution : 96,
277+
args : [],
246278
})
247279

248280
const device = opts.format === 'png' ? 'png16m' : 'jpeg'
249281
const args = [
282+
...opts.args,
250283
'-dQUIET',
251284
'-dNOPAUSE',
252285
'-dBATCH',

packages/ghoulscript/src/rpc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as core from './core'
2+
import RpcWorker from './rpc.worker?worker&inline'
23

34
export type Core = typeof core
45

@@ -24,7 +25,7 @@ let worker: Worker
2425

2526
export async function useWorkerRPC () {
2627
if (!worker)
27-
worker = new Worker(new URL('rpc.worker.mjs', import.meta.url), { type: 'module' })
28+
worker = new RpcWorker({ name: 'rpc-worker' })
2829

2930
return worker
3031
}

packages/ghoulscript/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"strict": true,
2424
"noUnusedLocals": true,
2525
"noUnusedParameters": true,
26-
"noFallthroughCasesInSwitch": true
26+
"noFallthroughCasesInSwitch": true,
27+
"types": ["vite/client"]
2728
},
2829
"include": ["src", "tests"]
2930
}

packages/ghoulscript/vite.config.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { defineConfig } from 'vite'
2+
import dts from 'vite-plugin-dts'
3+
import { nodePolyfills } from 'vite-plugin-node-polyfills'
4+
import { externalizeDeps } from 'vite-plugin-externalize-deps'
5+
6+
export default defineConfig({
7+
plugins: [
8+
dts({ rollupTypes: true }),
9+
nodePolyfills(),
10+
externalizeDeps(),
11+
],
12+
build: {
13+
minify: false,
14+
lib : {
15+
entry : 'src/index',
16+
name : 'Ghoulscript',
17+
formats: ['cjs', 'es'],
18+
fileName (format, entryName) {
19+
const EXT: Record<string, string> = {
20+
cjs: '.cjs',
21+
es : '.mjs',
22+
umd: '.umd.cjs',
23+
}
24+
25+
return `${entryName}${EXT[format] ?? '.js'}`
26+
},
27+
},
28+
},
29+
})

playground/sample/src/main.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function bytes (bytes: number, decimal = 2, k = 1024) {
99
return '0 Bytes'
1010

1111
const sizes = [
12-
`${bytes > 1 ? 'Bytes' : 'Byte'}`,
12+
`${bytes === 1 ? 'Byte' : 'Bytes'}`,
1313
'KB',
1414
'MB',
1515
'GB',
@@ -30,13 +30,14 @@ input?.addEventListener('change', async () => {
3030

3131
if (file) {
3232
try {
33-
const start = performance.now()
33+
const start = performance.now()
34+
const result = await optimizePDF(file)
3435

35-
const result = await optimizePDF(file) as Uint8Array
36-
const diff = Math.round((result.byteLength - file.size) / file.size * 100)
36+
const diffBytes = result.byteLength - file.size
37+
const diff = Math.round(diffBytes / file.size * 100)
3738

3839
if (log)
39-
log.textContent = `Result: ${bytes(file.size)} => ${bytes(result.byteLength)} (${diff}%), Duration: ${Math.round((performance.now() - start) / 1000)}s`
40+
log.textContent = `Result: ${bytes(file.size)} => ${bytes(result.byteLength)} (${diff}%: ${bytes(Math.abs(diffBytes))}), Duration: ${Math.round((performance.now() - start) / 1000)}s`
4041

4142
window.open(URL.createObjectURL(new Blob([result], { type: 'application/pdf' })), '_blank')
4243
} catch (error) {

playground/sample/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { defineConfig } from 'vite'
22

3-
export default defineConfig({ optimizeDeps: { exclude: ['@privyid/ghoulscript'] } })
3+
export default defineConfig({})

0 commit comments

Comments
 (0)