@@ -19,7 +19,7 @@ export interface CompressOptions {
1919 */
2020 pdfSettings : 'screen' | 'ebook' | 'printer' | 'prepress' | 'default' ,
2121 /**
22- * Enable Linearization
22+ * Enable Fast Web View ( Linearization)
2323 * @default true
2424 */
2525 fastWebView : boolean ,
@@ -63,8 +63,17 @@ export interface CompressOptions {
6363 pageList ?: PageList ,
6464}
6565
66- async function createPDF ( inputs : ArrayBufferView [ ] , options : Partial < CompressOptions > = { } ) : Promise < Uint8Array > {
67- const gs = await useGS ( )
66+ type InputFile = ArrayBufferView | Blob
67+
68+ async function readFile ( input : InputFile ) : Promise < ArrayBufferView > {
69+ if ( input instanceof globalThis . Blob )
70+ return new Uint8Array ( await input . arrayBuffer ( ) )
71+
72+ return input
73+ }
74+
75+ async function createPDF ( inputs : InputFile [ ] , options : Partial < CompressOptions > = { } ) : Promise < Uint8Array > {
76+ const gs = await useGS ( { print ( ) { } , printErr ( ) { } } )
6877 const opts = defu < CompressOptions , [ CompressOptions ] > ( options , {
6978 pdfSettings : 'screen' ,
7079 compatibilityLevel : '1.4' ,
@@ -129,7 +138,7 @@ async function createPDF (inputs: ArrayBufferView[], options: Partial<CompressOp
129138 for ( const [ i , input ] of inputs . entries ( ) ) {
130139 const inputFilename = `./input-${ i } `
131140
132- gs . FS . writeFile ( inputFilename , input )
141+ gs . FS . writeFile ( inputFilename , await readFile ( input ) )
133142 args . push ( inputFilename )
134143 }
135144
@@ -144,7 +153,7 @@ async function createPDF (inputs: ArrayBufferView[], options: Partial<CompressOp
144153 * @param option
145154 * @returns
146155 */
147- export async function optimizePDF ( input : ArrayBufferView , option : Partial < CompressOptions > = { } ) {
156+ export async function optimizePDF ( input : InputFile , option : Partial < CompressOptions > = { } ) : Promise < Uint8Array > {
148157 return await createPDF ( [ input ] , option )
149158}
150159
@@ -154,7 +163,7 @@ export async function optimizePDF (input: ArrayBufferView, option: Partial<Compr
154163 * @param option
155164 * @returns
156165 */
157- export async function combinePDF ( inputs : ArrayBufferView [ ] , option : Partial < CompressOptions > = { } ) {
166+ export async function combinePDF ( inputs : InputFile [ ] , option : Partial < CompressOptions > = { } ) : Promise < Uint8Array > {
158167 return await createPDF ( inputs , option )
159168}
160169
@@ -165,7 +174,7 @@ export async function combinePDF (inputs: ArrayBufferView[], option: Partial<Com
165174 * @param option
166175 * @returns
167176 */
168- export async function splitPdf ( input : ArrayBufferView , pageLists : PageList [ ] , option : Partial < CompressOptions > = { } ) {
177+ export async function splitPdf ( input : InputFile , pageLists : PageList [ ] , option : Partial < CompressOptions > = { } ) : Promise < Uint8Array [ ] > {
169178 return await Promise . all (
170179 pageLists . map ( async ( pageList ) => {
171180 return await createPDF ( [ input ] , defu ( { pageList } , option ) )
@@ -180,7 +189,7 @@ export async function splitPdf (input: ArrayBufferView, pageLists: PageList[], o
180189 * @param ownerPassword
181190 * @returns
182191 */
183- export async function addPassword ( input : ArrayBufferView , userPassword : string , ownerPassword : string = userPassword ) {
192+ export async function addPassword ( input : InputFile , userPassword : string , ownerPassword : string = userPassword ) : Promise < Uint8Array > {
184193 return await createPDF ( [ input ] , {
185194 ownerPassword,
186195 userPassword,
@@ -193,7 +202,7 @@ export async function addPassword (input: ArrayBufferView, userPassword: string,
193202 * @param password
194203 * @returns
195204 */
196- export async function removePassword ( input : ArrayBufferView , password : string ) {
205+ export async function removePassword ( input : InputFile , password : string ) : Promise < Uint8Array > {
197206 return await createPDF ( [ input ] , { keepPassword : false , password : password } )
198207}
199208
@@ -227,8 +236,8 @@ export interface RenderOptions {
227236 * @param options
228237 * @returns
229238 */
230- export async function renderPageAsImage ( input : ArrayBufferView , pageNumber : number = 1 , options : Partial < RenderOptions > = { } ) {
231- const gs = await useGS ( )
239+ export async function renderPageAsImage ( input : InputFile , pageNumber : number = 1 , options : Partial < RenderOptions > = { } ) : Promise < Uint8Array > {
240+ const gs = await useGS ( { print ( ) { } , printErr ( ) { } } )
232241 const opts = defu < RenderOptions , [ RenderOptions ] > ( options , {
233242 format : 'jpg' ,
234243 graphicsAlphaBits : 4 ,
@@ -251,7 +260,7 @@ export async function renderPageAsImage (input: ArrayBufferView, pageNumber: num
251260 './input' ,
252261 ]
253262
254- gs . FS . writeFile ( './input' , input )
263+ gs . FS . writeFile ( './input' , await readFile ( input ) )
255264
256265 await gs . callMain ( args )
257266
@@ -273,7 +282,7 @@ export interface Info {
273282 * @param options
274283 * @returns
275284 */
276- export async function getInfo ( input : ArrayBufferView , options : Pick < CompressOptions , 'password' > = { } ) : Promise < Info > {
285+ export async function getInfo ( input : InputFile , options : Pick < CompressOptions , 'password' > = { } ) : Promise < Info > {
277286 const info : Info = {
278287 numPages : 0 ,
279288 pages : [ ] ,
@@ -313,9 +322,42 @@ export async function getInfo (input: ArrayBufferView, options: Pick<CompressOpt
313322 if ( options . password )
314323 args . splice ( - 1 , 0 , `-sPDFPassword=${ options . password } ` )
315324
316- gs . FS . writeFile ( './input' , input )
325+ gs . FS . writeFile ( './input' , await readFile ( input ) )
317326
318327 await gs . callMain ( args )
319328
320329 return info
321330}
331+
332+ /**
333+ * Check document is encrypted using password or not
334+ * @param input
335+ * @returns - true if required
336+ */
337+ export async function isRequirePassword ( input : InputFile ) : Promise < boolean > {
338+ let result = false
339+
340+ const gs = await useGS ( {
341+ print ( ) { } ,
342+ printErr ( str ) {
343+ if ( str . match ( 'This file requires a password for access' ) )
344+ result = true
345+ } ,
346+ } )
347+
348+ const args = [
349+ '-dQUIET' ,
350+ '-dNOPAUSE' ,
351+ '-dNODISPLAY' ,
352+ '-dBATCH' ,
353+ '-dSAFER' ,
354+ '-dPDFINFO' ,
355+ './input' ,
356+ ]
357+
358+ gs . FS . writeFile ( './input' , await readFile ( input ) )
359+
360+ await gs . callMain ( args )
361+
362+ return result
363+ }
0 commit comments