feat(new tool): PDF Text Extractor

Fix #1572
This commit is contained in:
ShareVB committed 2025-06-08 12:12:57 +02:00
1 parent 78fdcebefd
commit 39479739e5
6 files changed
+113 -6

No files matched your search

+1
View File
@@ -271,6 +271,7 @@
"unicode-emoji-json": "^0.8.0",
"unique-random": "^4.0.0",
"unixcrypt-browser": "^2.0.4",
"unpdf": "^1.0.5",
"unplugin-auto-import": "^0.16.4",
"utf8": "^3.0.0",
"uuid": "^11.0.5",
+12
View File
@@ -710,6 +710,9 @@ dependencies:
unixcrypt-browser:
specifier: ^2.0.4
version: 2.0.4
unpdf:
specifier: ^1.0.5
version: 1.0.5
unplugin-auto-import:
specifier: ^0.16.4
version: 0.16.4(@vueuse/core@10.11.1)(rollup@2.79.2)
@@ -17773,6 +17776,15 @@ packages:
- supports-color
dev: true
/unpdf@1.0.5:
resolution: {integrity: sha512-f06b1bT0Cu96UpQmU4wQbir4nJrZ8kMU9dC3zUg7Ac+Sy5iKuh/aHq58rlyk6ggue0xjhed2Q5uu0AJVSl7qHA==}
peerDependencies:
'@napi-rs/canvas': ^0.1.69
peerDependenciesMeta:
'@napi-rs/canvas':
optional: true
dev: false
/unplugin-auto-import@0.16.4(@vueuse/core@10.11.1)(rollup@2.79.2):
resolution: {integrity: sha512-xdgBa9NAS3JG8HjkAZHSbGSMlrjKpaWKXGUzaF6RzEtr980RCl1t0Zsu0skUInNYrEQfqaHc7aGWPv41DLTK/w==}
engines: {node: '>=14'}
+3 -1
View File
@@ -72,7 +72,9 @@ const themeVars = useThemeVars();
</div>
<div class="tool-content">
<slot />
<Suspense>
<slot />
</Suspense>
</div>
<div class="tool-footer">
+13
View File
@@ -0,0 +1,13 @@
import { FileExport } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'PDF Text Extractor',
path: '/pdf-text-extractor',
description: 'Extract text from PDF',
keywords: ['pdf', 'text', 'extractor'],
component: () => import('./pdf-text-extractor.vue'),
icon: FileExport,
createdAt: new Date('2025-05-08'),
category: 'PDF',
});
@@ -0,0 +1,81 @@
<script setup lang="ts">
import { definePDFJSModule, extractText, getDocumentProxy } from 'unpdf';
import * as pdfJS from 'pdfjs-dist';
import pdfJSWorkerURL from 'pdfjs-dist/build/pdf.worker?url';
pdfJS.GlobalWorkerOptions.workerSrc = pdfJSWorkerURL;
// Define the PDF.js build before using any other unpdf method
await definePDFJSModule(() => import('pdfjs-dist'));
const status = ref<'idle' | 'done' | 'error' | 'processing'>('idle');
const file = ref<File | null>(null);
const totalPages = ref(0);
const text = ref('');
const errorMessage = ref('');
const mergePages = ref(false);
function onFileUploaded(uploadedFile: File) {
file.value = uploadedFile;
totalPages.value = 0;
text.value = '';
}
async function processPDF() {
if (!file.value) {
return;
}
status.value = 'idle';
errorMessage.value = '';
try {
status.value = 'processing';
const fileBuffer = await file.value.arrayBuffer();
const pdf = await getDocumentProxy(new Uint8Array(fileBuffer));
const data = await extractText(pdf, { mergePages: mergePages.value as never });
totalPages.value = data.totalPages;
text.value = Array.isArray(data.text) ? data.text.join('\n') : data.text;
status.value = 'done';
}
catch (e: any) {
errorMessage.value = e.toString();
status.value = 'error';
}
}
</script>
<template>
<div>
<div style="flex: 0 0 100%" mb-1>
<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>
</div>
<div flex justify-center>
<n-form-item label="Merge pages" label-placement="left">
<n-checkbox v-model:checked="mergePages" />
</n-form-item>
</div>
<div flex justify-center>
<c-button mb-1 :disabled="!file" @click="processPDF">
Extract text
</c-button>
</div>
<div mb-1 mt-3 flex justify-center>
<c-alert v-if="errorMessage" type="error">
{{ errorMessage }}
</c-alert>
<n-spin
v-if="status === 'processing'"
size="small"
/>
</div>
<c-card v-if="file && text" title="Output">
<input-copyable label="File name:" :value="file?.name" mb-1 />
<textarea-copyable label="Text:" :value="text" mb-1 />
<input-copyable label="Total Pages:" :value="totalPages.toString()" mb-1 />
</c-card>
</div>
</template>
+3 -5
View File
@@ -116,6 +116,7 @@ export default defineConfig({
'@babel/core': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
'isolated-vm': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
'onnxruntime-node': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
'unpdf/pdfjs': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
'webcrypto-liner-shim': !process.env.VERCEL ? 'webcrypto-liner-shim' : fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
},
},
@@ -133,9 +134,7 @@ export default defineConfig({
// cssMinify: false,
// modulePreload: false,
rollupOptions: {
external: ['regex', './out/isolated_vm', 'isolated-vm', 'onnxruntime-node',
...(process.env.VERCEL ? ['webcrypto-liner-shim'] : [])
],
external: ['regex', './out/isolated_vm', 'isolated-vm', 'onnxruntime-node', 'unpdf/pdfjs', ...(process.env.VERCEL ? ['webcrypto-liner-shim'] : [])],
output: {
format: 'es',
// manualChunks: (id) => {
@@ -157,8 +156,7 @@ export default defineConfig({
},
},
optimizeDeps: {
include: ['isolated-vm', 'pdfjs-dist', 'onnxruntime-node',
...(process.env.VERCEL ? ['webcrypto-liner-shim'] : [])], // optionally specify dependency name
include: ['isolated-vm', 'pdfjs-dist', 'onnxruntime-node', 'unpdf', 'unpdf/pdfjs', ...(process.env.VERCEL ? ['webcrypto-liner-shim'] : [])], // optionally specify dependency name
esbuildOptions: {
supported: {
'top-level-await': true,