1- import useGS from '@privyid/ghostscript'
1+ import type { GSModule } from '@privyid/ghostscript'
2+ import initGS from '@privyid/ghostscript'
23import { defu } from 'defu'
4+ import { getFileURL , useConfig } from './config'
5+ import { joinRelativeURL } from 'ufo'
36
47interface PageRange {
58 start : number ,
@@ -63,6 +66,17 @@ export interface CompressOptions {
6366 pageList ?: PageList ,
6467}
6568
69+ async function useGS ( moduleOverrides ?: Partial < GSModule > ) {
70+ return await initGS ( defu < Partial < GSModule > , [ Partial < GSModule > ] > ( moduleOverrides , {
71+ locateFile ( url : string , dir : string ) {
72+ if ( ( typeof window !== 'undefined' || typeof importScripts === 'function' ) && useConfig ( ) . useCDN )
73+ return getFileURL ( url )
74+
75+ return joinRelativeURL ( dir , url )
76+ } ,
77+ } ) )
78+ }
79+
6680async function createPDF ( inputs : ArrayBufferView [ ] , options : Partial < CompressOptions > = { } ) : Promise < Uint8Array > {
6781 const gs = await useGS ( )
6882 const opts = defu < CompressOptions , [ CompressOptions ] > ( options , {
@@ -138,14 +152,33 @@ async function createPDF (inputs: ArrayBufferView[], options: Partial<CompressOp
138152 return gs . FS . readFile ( './output' , { encoding : 'binary' } )
139153}
140154
155+ /**
156+ * Optimize PDF and redure file size
157+ * @param input
158+ * @param option
159+ * @returns
160+ */
141161export async function optimizePDF ( input : ArrayBufferView , option : Partial < CompressOptions > = { } ) {
142162 return await createPDF ( [ input ] , option )
143163}
144164
165+ /**
166+ * Merge multiple files into single file
167+ * @param inputs
168+ * @param option
169+ * @returns
170+ */
145171export async function combinePDF ( inputs : ArrayBufferView [ ] , option : Partial < CompressOptions > = { } ) {
146172 return await createPDF ( inputs , option )
147173}
148174
175+ /**
176+ * Split PDF into multiple files
177+ * @param input
178+ * @param pageLists
179+ * @param option
180+ * @returns
181+ */
149182export async function splitPdf ( input : ArrayBufferView , pageLists : PageList [ ] , option : Partial < CompressOptions > = { } ) {
150183 return await Promise . all (
151184 pageLists . map ( async ( pageList ) => {
@@ -154,6 +187,13 @@ export async function splitPdf (input: ArrayBufferView, pageLists: PageList[], o
154187 )
155188}
156189
190+ /**
191+ * Add encryption password
192+ * @param input
193+ * @param userPassword
194+ * @param ownerPassword
195+ * @returns
196+ */
157197export async function addPassword ( input : ArrayBufferView , userPassword : string , ownerPassword : string = userPassword ) {
158198 return await createPDF ( [ input ] , {
159199 ownerPassword,
@@ -194,6 +234,13 @@ export interface RenderOptions {
194234 format : 'jpg' | 'png' ,
195235}
196236
237+ /**
238+ * Convert PDF to image
239+ * @param input Input buffer
240+ * @param pageNumber
241+ * @param options
242+ * @returns
243+ */
197244export async function renderPageAsImage ( input : ArrayBufferView , pageNumber : number = 1 , options : Partial < RenderOptions > = { } ) {
198245 const gs = await useGS ( )
199246 const opts = defu < RenderOptions , [ RenderOptions ] > ( options , {
0 commit comments