Files
Stirling-PDF/shared-operations/src/wasm/pdfcpu/pdfcpu-wrapper.client.js
T

99 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-11-07 01:40:00 +01:00
// imports browserfs via index.html script-tag
2024-02-25 20:55:48 +01:00
import wasmUrl from '../../../public/wasm/pdfcpu/pdfcpu.wasm?url'
2023-10-18 01:20:31 +02:00
2023-11-07 01:40:00 +01:00
let wasmLocation = "/wasm/pdfcpu/";
2023-10-18 01:20:31 +02:00
let fs;
let Buffer;
2023-11-07 01:40:00 +01:00
// TODO: This can later be defered to load asynchronously
2023-10-18 01:20:31 +02:00
configureFs();
loadWasm();
function configureFs() {
BrowserFS.configure(
{
fs: "InMemory",
},
function (e) {
if (e) {
// An error happened!
throw e;
}
fs = BrowserFS.BFSRequire("fs");
Buffer = BrowserFS.BFSRequire("buffer").Buffer;
window.fs = fs;
window.Buffer = Buffer;
}
);
}
function loadWasm() {
2024-02-25 20:55:48 +01:00
import("./wasm_exec.js");
2023-10-18 01:20:31 +02:00
}
const runWasm = async (param) => {
if (window.cachedWasmResponse === undefined) {
2024-02-25 20:55:48 +01:00
const response = await fetch(wasmUrl);
2023-10-18 01:20:31 +02:00
const buffer = await response.arrayBuffer();
window.cachedWasmResponse = buffer;
window.go = new Go();
}
const { instance } = await WebAssembly.instantiate(
window.cachedWasmResponse,
window.go.importObject
);
window.go.argv = param;
await window.go.run(instance);
return window.go.exitCode;
};
async function loadFileAsync(data) {
console.log(`Writing file to MemoryFS`);
await fs.writeFile(`/input.pdf`, data);
console.log(`Write done. Validating...`);
let exitcode = await runWasm([
2023-10-18 01:20:31 +02:00
"pdfcpu.wasm",
"validate",
"-c",
"disable",
`/input.pdf`,
]);
2024-02-27 21:51:03 +01:00
console.log("Exit Code: " + exitcode);
if (exitcode !== 0)
2023-10-18 01:20:31 +02:00
throw new Error("There was an error validating your PDFs");
console.log(`File is Valid`);
2023-10-18 01:20:31 +02:00
}
export async function oneToOne(wasmArray, snapshot) {
2023-10-18 01:20:31 +02:00
await loadFileAsync(Buffer.from(snapshot));
2023-10-22 01:11:17 +02:00
console.log("Nuping File");
let exitcode = await runWasm(wasmArray);
2023-10-18 01:20:31 +02:00
if (exitcode !== 0) {
console.error("There was an error nuping your PDFs");
return;
}
await fs.unlink("input.pdf");
const contents = fs.readFileSync("output.pdf");
fs.unlink("output.pdf");
console.log("Your File ist Ready!");
return new Uint8Array(contents);
}
export async function manyToOne() {
//TODO: Do this of neccesary for some operations
}
export async function oneToMany() {
//TODO: Do this of neccesary for some operations
}
export async function manyToMany() {
//TODO: Do this of neccesary for some operations
}