feat(new tool): Pdf Compressor

Fix #1672
This commit is contained in:
ShareVB committed 2025-11-29 12:50:25 +01:00
1 parent c25ec86546
commit 4db9743221
5 files changed
+161

No files matched your search

+1
View File
@@ -170,6 +170,7 @@
"fuse.js": "^7.1.0",
"generate-schema": "^2.6.0",
"get-timezone-offset": "^1.0.5",
"ghostscript-wasm-esm": "^1.0.1",
"gtin-validator": "^1.0.0",
"hash-wasm": "^4.12.0",
"haversine": "^1.1.1",
+10
View File
@@ -398,6 +398,9 @@ importers:
get-timezone-offset:
specifier: ^1.0.5
version: 1.0.5
ghostscript-wasm-esm:
specifier: ^1.0.1
version: 1.0.1
gtin-validator:
specifier: ^1.0.0
version: 1.0.0
@@ -6986,6 +6989,9 @@ packages:
getpass@0.1.7:
resolution: {integrity: sha512-0fzj9JxOLfJ+XGLhR8ze3unN0KZCgZwiSSDz168VERjK8Wl8kVSdcu2kspd4s4wtAa1y/qrVRiAA0WclVsu0ng==}
ghostscript-wasm-esm@1.0.1:
resolution: {integrity: sha512-b4/nIROy6hygl9IWjIrwaaPo83ziUClo+1QOdHM0zjxOZ1QyfIrCR7EWW8HJO1GOe68GEBVLBCy9QlkgOGaRFQ==}
gifwrap@0.9.4:
resolution: {integrity: sha512-MDMwbhASQuVeD4JKd1fKgNgCRL3fGqMM4WaqpNhWO0JiMOAjbQdumbs4BbBZEy9/M00EHEjKN3HieVhCUlwjeQ==}
@@ -18450,6 +18456,10 @@ snapshots:
dependencies:
assert-plus: 1.0.0
ghostscript-wasm-esm@1.0.1:
dependencies:
'@types/emscripten': 1.40.1
gifwrap@0.9.4:
dependencies:
image-q: 4.0.0
BIN
View File
Binary file not shown.
+13
View File
@@ -0,0 +1,13 @@
import { FileZip } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'PDF Compressor',
path: '/pdf-compressor',
description: 'Optimize and compress PDF using Ghostscript',
keywords: ['pdf', 'optimize', 'compressor'],
component: () => import('./pdf-compressor.vue'),
icon: FileZip,
createdAt: new Date('2025-11-11'),
category: 'PDF',
});
+137
View File
@@ -0,0 +1,137 @@
<script setup lang="ts">
import { Base64 } from 'js-base64';
import createGSModule from 'ghostscript-wasm-esm';
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
const quality = ref('screen');
const compatibility = ref('1.4');
const qualityOptions = [
{ label: 'Screen (smallest)', value: 'screen' },
{ label: 'eBook (balanced)', value: 'ebook' },
{ label: 'Printer (high quality)', value: 'printer' },
{ label: 'Prepress (best quality)', value: 'prepress' },
];
const compatibilityOptions = [
{ label: 'PDF 1.4', value: '1.4' },
{ label: 'PDF 1.5', value: '1.5' },
{ label: 'PDF 1.6', value: '1.6' },
{ label: 'PDF 1.7', value: '1.7' },
];
const status = ref<'idle' | 'done' | 'error' | 'processing'>('idle');
const file = ref<File | null>(null);
const base64OutputPDF = ref('');
const logs = ref<string[]>([]);
const fileName = ref('');
const fileExtension = ref('pdf');
const { download } = useDownloadFileFromBase64(
{
source: base64OutputPDF,
filename: fileName,
extension: fileExtension,
});
const gsCommand = ref('');
async function onFileUploaded(uploadedFile: File) {
file.value = uploadedFile;
const fileBuffer = await uploadedFile.arrayBuffer();
fileName.value = `compressed_${uploadedFile.name}`;
status.value = 'processing';
try {
const outPdfBuffer = await callMainWithInOutPdf(fileBuffer,
[
'-sDEVICE=pdfwrite',
`-dCompatibilityLevel=${compatibility.value}`,
`-dPDFSETTINGS=/${quality.value}`,
'-dNOPAUSE',
'-dQUIET',
'-dBATCH',
'-sOutputFile=out.pdf',
'in.pdf',
],
0);
base64OutputPDF.value = `data:application/pdf;base64,${Base64.fromUint8Array(outPdfBuffer)}`;
status.value = 'done';
download();
}
catch (e) {
status.value = 'error';
}
}
const base = import.meta.env.BASE_URL ?? '/';
async function callMainWithInOutPdf(data: ArrayBuffer, args: string[], expected_exitcode: number) {
gsCommand.value = args.join(' ');
logs.value = [];
const mod = await createGSModule({
locateFile(file: string) {
return base + file;
},
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>
<c-select
v-model:value="quality"
label="Compression quality:"
label-position="left"
label-width="150px"
:options="qualityOptions"
placeholder="Select compression quality"
:disabled="status === 'processing'"
mb-1
/>
<c-select
v-model:value="compatibility"
label="Compatibility level:"
label-position="left"
label-width="150px"
:options="compatibilityOptions"
placeholder="Select compatibility level"
:disabled="status === 'processing'"
mb-4
/>
<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>
</div>
<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">
<input-copyable label="Ghostscript" :value="gsCommand" mb-1 />
<pre>{{ logs.join('\n') }}</pre>
</c-card>
</div>
</template>