|
| 1 | +<script setup lang="ts"> |
| 2 | +import { Base64 } from 'js-base64'; |
| 3 | +import createQPDFModule from '@/libs/qpdf/qpdf'; |
| 4 | +import { useDownloadFileFromBase64Refs } from '@/composable/downloadBase64'; |
| 5 | +
|
| 6 | +const status = ref<'idle' | 'done' | 'error' | 'processing'>('idle'); |
| 7 | +const file = ref<File | null>(null); |
| 8 | +
|
| 9 | +const base64OutputPDF = ref(''); |
| 10 | +const fileName = ref(''); |
| 11 | +const fileExtension = ref('pdf'); |
| 12 | +const { download } = useDownloadFileFromBase64Refs( |
| 13 | + { |
| 14 | + source: base64OutputPDF, |
| 15 | + filename: fileName, |
| 16 | + extension: fileExtension, |
| 17 | + }); |
| 18 | +
|
| 19 | +async function onProcessClicked(uploadedFile: File) { |
| 20 | + file.value = uploadedFile; |
| 21 | + const fileBuffer = await uploadedFile.arrayBuffer(); |
| 22 | +
|
| 23 | + fileName.value = `decrypted_${uploadedFile.name}`; |
| 24 | + status.value = 'processing'; |
| 25 | + try { |
| 26 | + const outPdfBuffer = await callMainWithInOutPdf(fileBuffer, |
| 27 | + [ |
| 28 | + '--decrypt', |
| 29 | + 'in.pdf', |
| 30 | + 'out.pdf', |
| 31 | + ], |
| 32 | + 0); |
| 33 | + base64OutputPDF.value = `data:application/pdf;base64,${Base64.fromUint8Array(outPdfBuffer)}`; |
| 34 | + status.value = 'done'; |
| 35 | +
|
| 36 | + download(); |
| 37 | + } |
| 38 | + catch (e) { |
| 39 | + status.value = 'error'; |
| 40 | + } |
| 41 | +} |
| 42 | +
|
| 43 | +async function callMainWithInOutPdf(data: ArrayBuffer, args: string[], expected_exitcode: number) { |
| 44 | + const mod = await createQPDFModule(); |
| 45 | + mod.FS.writeFile('in.pdf', new Uint8Array(data)); |
| 46 | + const ret = mod.callMain(args); |
| 47 | + if (expected_exitcode !== ret) { |
| 48 | + throw new Error('Process run failed'); |
| 49 | + } |
| 50 | + return mod.FS.readFile('out.pdf'); |
| 51 | +} |
| 52 | +</script> |
| 53 | + |
| 54 | +<template> |
| 55 | + <div> |
| 56 | + <div style="flex: 0 0 100%"> |
| 57 | + <div mx-auto max-w-600px> |
| 58 | + <c-file-upload title="Drag and drop a PDF file here, or click to select a file" accept=".pdf" @file-upload="onProcessClicked" /> |
| 59 | + </div> |
| 60 | + </div> |
| 61 | + |
| 62 | + <div mt-3 flex justify-center> |
| 63 | + <c-alert v-if="status === 'error'" type="error"> |
| 64 | + An error occured processing {{ fileName }} |
| 65 | + </c-alert> |
| 66 | + <n-spin |
| 67 | + v-if="status === 'processing'" |
| 68 | + size="small" |
| 69 | + /> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | +</template> |
0 commit comments