diff --git a/public/testWorkflow.js b/public/exampleWorkflows.js
similarity index 84%
rename from public/testWorkflow.js
rename to public/exampleWorkflows.js
index 673dfbc3..61563483 100644
--- a/public/testWorkflow.js
+++ b/public/exampleWorkflows.js
@@ -1,8 +1,7 @@
// JSON Representation of this Node Tree:
// https://discord.com/channels/1068636748814483718/1099390571493195898/1118192754103693483
// https://cdn.discordapp.com/attachments/1099390571493195898/1118192753759764520/image.png?ex=6537dba7&is=652566a7&hm=dc46820ef7c34bc37424794966c5f66f93ba0e15a740742c364d47d31ea119a9&
-
-export const testWorkflow = {
+export const discordWorkflow = {
outputOptions: {
zip: false,
awaitAllDone: true
@@ -45,7 +44,7 @@ export const testWorkflow = {
values: { "id": 1 },
operations: [
{
- type: "merge", // This gets called when the other merge-ops with the same id finish.
+ type: "merge",
values: {},
operations: []
}
@@ -57,7 +56,7 @@ export const testWorkflow = {
operations: []
},
{
- type: "merge", // This gets called when the other merge-ops with the same id finish.
+ type: "merge",
values: {},
operations: [
{
@@ -68,4 +67,19 @@ export const testWorkflow = {
]
}
]
+}
+
+// This will merge all input files into one giant document
+export const mergeOnly = {
+ outputOptions: {
+ zip: false,
+ awaitAllDone: true
+ },
+ operations: [
+ {
+ type: "merge",
+ values: {},
+ operations: []
+ }
+ ]
}
\ No newline at end of file
diff --git a/public/functions/mergePDFs.js b/public/functions/mergePDFs.js
new file mode 100644
index 00000000..f243451a
--- /dev/null
+++ b/public/functions/mergePDFs.js
@@ -0,0 +1,15 @@
+const { PDFDocument, ParseSpeeds } = PDFLib;
+
+export const mergePDFs = async (snapshots) => {
+
+ const mergedPdf = await PDFDocument.create();
+
+ for (let i = 0; i < snapshots.length; i++) {
+ const pdfToMerge = await PDFDocument.load(snapshots[i]);
+
+ const copiedPages = await mergedPdf.copyPages(pdfToMerge, pdfToMerge.getPageIndices());
+ copiedPages.forEach((page) => mergedPdf.addPage(page));
+ }
+
+ return mergedPdf.save();
+};
\ No newline at end of file
diff --git a/public/index.html b/public/index.html
index ac937236..821533bc 100644
--- a/public/index.html
+++ b/public/index.html
@@ -11,7 +11,7 @@
-
+
diff --git a/public/index.js b/public/index.js
index 8811c40d..d109efd2 100644
--- a/public/index.js
+++ b/public/index.js
@@ -1,6 +1,6 @@
import { scaleContent } from "./functions/scaleContent.js";
import { scalePage, PageSize } from "./functions/scalePage.js";
-import { testWorkflow } from "./testWorkflow.js";
+import * as exampleWorkflows from "./exampleWorkflows.js";
import { traverseOperations } from "./traverseOperations.js";
(async (workflow) => {
@@ -10,22 +10,22 @@ import { traverseOperations } from "./traverseOperations.js";
doneButton.addEventListener('click', async (e) => {
const files = Array.from(pdfFileInput.files);
console.log(files);
- const pdfBuffers = await Promise.all(files.map(async file => {
+ const inputs = await Promise.all(files.map(async file => {
return {
originalFileName: file.name.replace(/\.[^/.]+$/, ""),
fileName: file.name.replace(/\.[^/.]+$/, ""),
buffer: new Uint8Array(await file.arrayBuffer())
}
}));
- console.log(pdfBuffers);
+ console.log(inputs);
- await traverseOperations(workflow.operations, pdfBuffers);
+ // TODO: This can also be run serverside
+ const results = await traverseOperations(workflow.operations, inputs);
+
+ results.forEach(result => {
+ download(result.buffer, result.fileName, "application/pdf");
+ });
- // if(selectedElementsList[0].textContent == "mergePDFs") {
-
- // }
-
- // // TODO: This can also be run serverside
// if(files.length > 1) {
// files.forEach(file => {
@@ -55,4 +55,4 @@ import { traverseOperations } from "./traverseOperations.js";
// }
// }
});
-})(testWorkflow);
+})(exampleWorkflows.mergeOnly);
diff --git a/public/traverseOperations.js b/public/traverseOperations.js
index 50ad7ad6..b02edf42 100644
--- a/public/traverseOperations.js
+++ b/public/traverseOperations.js
@@ -1,15 +1,16 @@
+import { mergePDFs } from "./functions/mergePDFs.js";
import { organizeWaitOperations } from "./organizeWaitOperations.js";
export async function traverseOperations(operations, input) {
const waitOperations = organizeWaitOperations(operations);
+ const results = [];
await nextOperation(operations, input);
+ return results;
async function nextOperation(operations, input) {
if(Array.isArray(operations) && operations.length == 0) { // isEmpty
console.log("operation done: " + input.fileName);
-
- //TODO: Delay the download
- download(input, input.fileName, "application/pdf");
+ results.push(input);
return;
}
@@ -89,15 +90,16 @@ export async function traverseOperations(operations, input) {
}
break;
case "merge":
- if(Array.isArray(input)) {
+ if(Array.isArray(input) && input.length > 1) {
+ const inputs = input;
input = {
- originalFileName: input.map(input => input.originalFileName).join("_and_"),
- fileName: input.map(input => input.fileName).join("_and_") + "_merged",
- buffer: input[0].buffer // TODO: merge inputs
+ originalFileName: inputs.map(input => input.originalFileName).join("_and_"),
+ fileName: inputs.map(input => input.fileName).join("_and_") + "_merged",
+ buffer: await mergePDFs(inputs.map(input => input.buffer))
}
}
else {
- // TODO: modfiy input
+ // Only one input, no need to merge
input.fileName += "_merged";
}
await nextOperation(operation.operations, input);