mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-02-02 00:16:34 +01:00
Fixed downloading buffer, merging workflow
This commit is contained in:
parent
96bac91fa1
commit
8e8c4596bf
@ -1,8 +1,7 @@
|
|||||||
// JSON Representation of this Node Tree:
|
// JSON Representation of this Node Tree:
|
||||||
// https://discord.com/channels/1068636748814483718/1099390571493195898/1118192754103693483
|
// https://discord.com/channels/1068636748814483718/1099390571493195898/1118192754103693483
|
||||||
// https://cdn.discordapp.com/attachments/1099390571493195898/1118192753759764520/image.png?ex=6537dba7&is=652566a7&hm=dc46820ef7c34bc37424794966c5f66f93ba0e15a740742c364d47d31ea119a9&
|
// https://cdn.discordapp.com/attachments/1099390571493195898/1118192753759764520/image.png?ex=6537dba7&is=652566a7&hm=dc46820ef7c34bc37424794966c5f66f93ba0e15a740742c364d47d31ea119a9&
|
||||||
|
export const discordWorkflow = {
|
||||||
export const testWorkflow = {
|
|
||||||
outputOptions: {
|
outputOptions: {
|
||||||
zip: false,
|
zip: false,
|
||||||
awaitAllDone: true
|
awaitAllDone: true
|
||||||
@ -45,7 +44,7 @@ export const testWorkflow = {
|
|||||||
values: { "id": 1 },
|
values: { "id": 1 },
|
||||||
operations: [
|
operations: [
|
||||||
{
|
{
|
||||||
type: "merge", // This gets called when the other merge-ops with the same id finish.
|
type: "merge",
|
||||||
values: {},
|
values: {},
|
||||||
operations: []
|
operations: []
|
||||||
}
|
}
|
||||||
@ -57,7 +56,7 @@ export const testWorkflow = {
|
|||||||
operations: []
|
operations: []
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: "merge", // This gets called when the other merge-ops with the same id finish.
|
type: "merge",
|
||||||
values: {},
|
values: {},
|
||||||
operations: [
|
operations: [
|
||||||
{
|
{
|
||||||
@ -69,3 +68,18 @@ 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: []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
15
public/functions/mergePDFs.js
Normal file
15
public/functions/mergePDFs.js
Normal file
@ -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();
|
||||||
|
};
|
@ -11,7 +11,7 @@
|
|||||||
<script src="index.js" type="module"></script>
|
<script src="index.js" type="module"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<input type="file" id="pdfFile" accept=".pdf">
|
<input type="file" id="pdfFile" accept=".pdf" multiple>
|
||||||
|
|
||||||
<ul id="operations">
|
<ul id="operations">
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { scaleContent } from "./functions/scaleContent.js";
|
import { scaleContent } from "./functions/scaleContent.js";
|
||||||
import { scalePage, PageSize } from "./functions/scalePage.js";
|
import { scalePage, PageSize } from "./functions/scalePage.js";
|
||||||
import { testWorkflow } from "./testWorkflow.js";
|
import * as exampleWorkflows from "./exampleWorkflows.js";
|
||||||
import { traverseOperations } from "./traverseOperations.js";
|
import { traverseOperations } from "./traverseOperations.js";
|
||||||
|
|
||||||
(async (workflow) => {
|
(async (workflow) => {
|
||||||
@ -10,22 +10,22 @@ import { traverseOperations } from "./traverseOperations.js";
|
|||||||
doneButton.addEventListener('click', async (e) => {
|
doneButton.addEventListener('click', async (e) => {
|
||||||
const files = Array.from(pdfFileInput.files);
|
const files = Array.from(pdfFileInput.files);
|
||||||
console.log(files);
|
console.log(files);
|
||||||
const pdfBuffers = await Promise.all(files.map(async file => {
|
const inputs = await Promise.all(files.map(async file => {
|
||||||
return {
|
return {
|
||||||
originalFileName: file.name.replace(/\.[^/.]+$/, ""),
|
originalFileName: file.name.replace(/\.[^/.]+$/, ""),
|
||||||
fileName: file.name.replace(/\.[^/.]+$/, ""),
|
fileName: file.name.replace(/\.[^/.]+$/, ""),
|
||||||
buffer: new Uint8Array(await file.arrayBuffer())
|
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);
|
||||||
|
|
||||||
// if(selectedElementsList[0].textContent == "mergePDFs") {
|
results.forEach(result => {
|
||||||
|
download(result.buffer, result.fileName, "application/pdf");
|
||||||
|
});
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // TODO: This can also be run serverside
|
|
||||||
// if(files.length > 1) {
|
// if(files.length > 1) {
|
||||||
// files.forEach(file => {
|
// files.forEach(file => {
|
||||||
|
|
||||||
@ -55,4 +55,4 @@ import { traverseOperations } from "./traverseOperations.js";
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
});
|
});
|
||||||
})(testWorkflow);
|
})(exampleWorkflows.mergeOnly);
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
|
import { mergePDFs } from "./functions/mergePDFs.js";
|
||||||
import { organizeWaitOperations } from "./organizeWaitOperations.js";
|
import { organizeWaitOperations } from "./organizeWaitOperations.js";
|
||||||
|
|
||||||
export async function traverseOperations(operations, input) {
|
export async function traverseOperations(operations, input) {
|
||||||
const waitOperations = organizeWaitOperations(operations);
|
const waitOperations = organizeWaitOperations(operations);
|
||||||
|
const results = [];
|
||||||
await nextOperation(operations, input);
|
await nextOperation(operations, input);
|
||||||
|
return results;
|
||||||
|
|
||||||
async function nextOperation(operations, input) {
|
async function nextOperation(operations, input) {
|
||||||
if(Array.isArray(operations) && operations.length == 0) { // isEmpty
|
if(Array.isArray(operations) && operations.length == 0) { // isEmpty
|
||||||
console.log("operation done: " + input.fileName);
|
console.log("operation done: " + input.fileName);
|
||||||
|
results.push(input);
|
||||||
//TODO: Delay the download
|
|
||||||
download(input, input.fileName, "application/pdf");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,15 +90,16 @@ export async function traverseOperations(operations, input) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "merge":
|
case "merge":
|
||||||
if(Array.isArray(input)) {
|
if(Array.isArray(input) && input.length > 1) {
|
||||||
|
const inputs = input;
|
||||||
input = {
|
input = {
|
||||||
originalFileName: input.map(input => input.originalFileName).join("_and_"),
|
originalFileName: inputs.map(input => input.originalFileName).join("_and_"),
|
||||||
fileName: input.map(input => input.fileName).join("_and_") + "_merged",
|
fileName: inputs.map(input => input.fileName).join("_and_") + "_merged",
|
||||||
buffer: input[0].buffer // TODO: merge inputs
|
buffer: await mergePDFs(inputs.map(input => input.buffer))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// TODO: modfiy input
|
// Only one input, no need to merge
|
||||||
input.fileName += "_merged";
|
input.fileName += "_merged";
|
||||||
}
|
}
|
||||||
await nextOperation(operation.operations, input);
|
await nextOperation(operation.operations, input);
|
||||||
|
Loading…
Reference in New Issue
Block a user