Skip to content

Commit 79b3961

Browse files
committed
Merge branch 'feat/pdf-fastweb' into chore/all-my-stuffs
# Conflicts: # components.d.ts # package.json # pnpm-lock.yaml # src/tools/index.ts
2 parents b1fc3fe + 81d1ccc commit 79b3961

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ import { tool as macAddressLookup } from './mac-address-lookup';
146146
import { tool as xmlFormatter } from './xml-formatter';
147147
import { tool as dockerComposeToDockerRunConverter } from './docker-compose-to-docker-run-converter';
148148
import { tool as pdfEncrypt } from './pdf-encrypt';
149+
import { tool as pdfLinearize } from './pdf-linearize';
149150
import { tool as yamlViewer } from './yaml-viewer';
150151
import { tool as barcodeReader } from './barcode-reader';
151152
import { tool as barcodeGenerator } from './barcode-generator';
@@ -174,6 +175,9 @@ export const toolsByCategory: ToolCategory[] = [
174175
passwordStrengthAnalyser,
175176
pdfSignatureChecker,
176177
pdfEncrypt,
178+
passwordStrengthAnalyser,
179+
pdfSignatureChecker,
180+
pdfLinearize,
177181
],
178182
},
179183
{

src/tools/pdf-linearize/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { FileLike } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Pdf Linearize/FastWeb',
6+
path: '/pdf-linearize',
7+
description: 'Create a Linearized version of a PDF (turn to FastWeb)',
8+
keywords: ['pdf', 'linearize', 'fastweb'],
9+
component: () => import('./pdf-linearize.vue'),
10+
icon: FileLike,
11+
createdAt: new Date('2024-01-09'),
12+
});
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<script setup lang="ts">
2+
import { Base64 } from 'js-base64';
3+
import createQPDFModule from 'qpdf-wasm-esm-embedded';
4+
import { useDownloadFileFromBase64 } 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 logs = ref<string[]>([]);
11+
const fileName = ref('');
12+
const fileExtension = ref('pdf');
13+
const { download } = useDownloadFileFromBase64(
14+
{
15+
source: base64OutputPDF,
16+
filename: fileName,
17+
extension: fileExtension,
18+
});
19+
20+
async function onFileUploaded(uploadedFile: File) {
21+
file.value = uploadedFile;
22+
const fileBuffer = await uploadedFile.arrayBuffer();
23+
24+
fileName.value = `linearized_${uploadedFile.name}`;
25+
status.value = 'processing';
26+
try {
27+
const outPdfBuffer = await callMainWithInOutPdf(fileBuffer,
28+
[
29+
'--linearize',
30+
'--warning-exit-0',
31+
'--verbose',
32+
'in.pdf',
33+
'out.pdf',
34+
],
35+
0);
36+
base64OutputPDF.value = `data:application/pdf;base64,${Base64.fromUint8Array(outPdfBuffer)}`;
37+
status.value = 'done';
38+
39+
download();
40+
}
41+
catch (e) {
42+
status.value = 'error';
43+
}
44+
}
45+
46+
async function callMainWithInOutPdf(data: ArrayBuffer, args: string[], expected_exitcode: number) {
47+
logs.value = [];
48+
const mod = await createQPDFModule({
49+
print(text: string) {
50+
logs.value.push(text);
51+
},
52+
printErr(text: string) {
53+
logs.value.push(text);
54+
},
55+
});
56+
mod.FS.writeFile('in.pdf', new Uint8Array(data));
57+
const ret = mod.callMain(args);
58+
if (expected_exitcode !== ret) {
59+
throw new Error('Process run failed');
60+
}
61+
return mod.FS.readFile('out.pdf');
62+
}
63+
</script>
64+
65+
<template>
66+
<div>
67+
<div style="flex: 0 0 100%">
68+
<div mx-auto max-w-600px>
69+
<c-file-upload title="Drag and drop a PDF file here, or click to select a file" accept=".pdf" @file-upload="onFileUploaded" />
70+
</div>
71+
</div>
72+
73+
<div mt-3 flex justify-center>
74+
<c-alert v-if="status === 'error'" type="error">
75+
An error occured processing {{ fileName }}
76+
</c-alert>
77+
<n-spin
78+
v-if="status === 'processing'"
79+
size="small"
80+
/>
81+
</div>
82+
83+
<c-card title="Logs">
84+
<pre>{{ logs.join('\n') }}</pre>
85+
</c-card>
86+
</div>
87+
</template>

0 commit comments

Comments
 (0)