feat(new tool): Pdf Extractor

Fix #52
This commit is contained in:
ShareVB committed 2025-05-01 20:22:01 +02:00
1 parent 2c12e1e38c
commit 2160e7a894
2 files changed
+138

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
import { FileExport } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Pdf Page Extractor',
path: '/pdf-extract',
description: 'Extract pages range from a PDF',
keywords: ['pdf', 'page', 'extract'],
component: () => import('./pdf-extract.vue'),
icon: FileExport,
createdAt: new Date('2025-03-09'),
category: 'PDF',
});
+125
View File
@@ -0,0 +1,125 @@
<script setup lang="ts">
import { Base64 } from 'js-base64';
import createQPDFModule from 'qpdf-wasm-esm-embedded';
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
const status = ref<'idle' | 'done' | 'error' | 'processing'>('idle');
const file = ref<File | null>(null);
const extractRange = ref('');
const base64OutputPDF = ref('');
const logs = ref<string[]>([]);
const fileName = ref('');
const { download } = useDownloadFileFromBase64(
{
source: base64OutputPDF,
filename: fileName,
extension: 'pdf',
});
function onFileUploaded(uploadedFile: File) {
file.value = uploadedFile;
fileName.value = `extract_${file.value.name}`;
}
async function onProcessClicked() {
if (!file.value) {
return;
}
const fileBuffer = await file.value.arrayBuffer();
status.value = 'processing';
try {
const options = [
'in.pdf',
'--verbose',
'--pages',
'.',
`${extractRange.value}`,
'--',
'out.pdf',
];
const outPdfBuffer = await callMainWithInOutPdf(fileBuffer,
options, 0);
base64OutputPDF.value = `data:application/pdf;base64,${Base64.fromUint8Array(outPdfBuffer)}`;
status.value = 'done';
download();
}
catch (e) {
status.value = 'error';
}
}
async function callMainWithInOutPdf(data: ArrayBuffer, args: string[], expected_exitcode: number) {
logs.value = [];
const mod = await createQPDFModule({
print(text: string) {
logs.value.push(text);
},
printErr(text: string) {
logs.value.push(text);
},
});
mod.FS.writeFile('in.pdf', new Uint8Array(data));
const ret = mod.callMain(args);
if (expected_exitcode !== ret) {
throw new Error('Process run failed');
}
return mod.FS.readFile('out.pdf');
}
</script>
<template>
<div>
<div style="flex: 0 0 100%">
<div mx-auto max-w-600px>
<c-file-upload
title="Drag and drop a PDF file here, or click to select a file"
accept=".pdf"
@file-upload="onFileUploaded"
/>
<div mt-2 text-center>
<c-input-text :value="fileName" label="Output file:" label-position="left" />
</div>
</div>
</div>
<c-card title="Extraction Range" mb-3 mt-3>
<c-input-text
v-model:value="extractRange"
label="Range:"
placeholder="Qpdf Range"
mb-2
/>
<n-p>
For details about Range options, see <n-a target="_blank" href="https://qpdf.readthedocs.io/en/stable/cli.html#page-selection">
QPDF Documentation
</n-a>
</n-p>
</c-card>
<div mt-3 flex justify-center>
<c-button :disabled="!file" @click="onProcessClicked()">
Extract PDF pages
</c-button>
</div>
<n-divider />
<div mt-3 flex justify-center>
<c-alert v-if="status === 'error'" type="error">
An error occured processing {{ fileName }}
</c-alert>
<n-spin
v-if="status === 'processing'"
size="small"
/>
</div>
<c-card title="Logs">
<pre>{{ logs.join('\n') }}</pre>
</c-card>
</div>
</template>