mirror of
				https://github.com/Frooodle/Stirling-PDF.git
				synced 2025-10-25 11:17:28 +02: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:
 | ||||
| // 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: [] | ||||
|         } | ||||
|     ] | ||||
| } | ||||
							
								
								
									
										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> | ||||
| </head> | ||||
| <body> | ||||
|     <input type="file" id="pdfFile" accept=".pdf"> | ||||
|     <input type="file" id="pdfFile" accept=".pdf" multiple> | ||||
| 
 | ||||
|     <ul id="operations"> | ||||
| 
 | ||||
|  | ||||
| @ -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); | ||||
|  | ||||
| @ -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); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user