From 025d692296ce7b59b80a1ae7e5be4bf85b9b04da Mon Sep 17 00:00:00 2001 From: Cocoa Date: Wed, 21 Feb 2024 18:46:49 -0500 Subject: [PATCH] Refactor pdf logic --- src/book.js | 71 ++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/src/book.js b/src/book.js index 417dfe1..5b3c466 100644 --- a/src/book.js +++ b/src/book.js @@ -319,23 +319,21 @@ export class Book { const generateSignatures = this.print_file != 'aggregated'; let aggregatePdf0, aggregatePdf1; if (generateAggregate) { - aggregatePdf0 = await PDFDocument.create(); - aggregatePdf1 = this.duplex ? null : await PDFDocument.create(); + aggregatePdf0 = await PDFDocument.create(); + aggregatePdf1 = this.duplex ? null : await PDFDocument.create(); } const forLoop = async () => { - for (let i = 0; i < this.rearrangedpages.length; i++) { const signature = this.rearrangedpages[i]; console.log(signature); const sigName = `${this.filename}_signature${i}`; const [sigFront, sigBack] = await this.createSignatures({ pageIndexDetails: signature, - fileList: this.filelist, }); if (this.duplex) { // collate - const sig = this.collatePages(sigFront, sigBack) + const sig = await this.collatePages(sigFront, sigBack); if (generateSignatures) { await sig.save().then((pdfBytes) => { @@ -344,10 +342,9 @@ export class Book { } if (generateAggregate) { - const copiedPages = await aggregatePdf0.embedPages(sig, sig.getPageIndices()); + const copiedPages = await aggregatePdf0.copyPages(sig, sig.getPageIndices()); copiedPages.forEach((page) => aggregatePdf0.addPage(page)); } - } else { if (generateSignatures) { await sigFront.save().then((pdfBytes) => { @@ -359,15 +356,19 @@ export class Book { } if (generateAggregate) { - const copiedPagesFront = await aggregatePdf0.embedPages(sigFront, sigFront.getPageIndices()); + const copiedPagesFront = await aggregatePdf0.copyPages( + sigFront, + sigFront.getPageIndices() + ); copiedPagesFront.forEach((page) => aggregatePdf0.addPage(page)); - const copiedPagesBack = await aggregatePdf1.embedPages(sigBack, sigBack.getPageIndices()); + const copiedPagesBack = await aggregatePdf1.copyPages( + sigBack, + sigBack.getPageIndices() + ); copiedPagesBack.forEach((page) => aggregatePdf1.addPage(page)); } - } - } }; await forLoop(); @@ -460,22 +461,27 @@ export class Book { return [newPdf, embeddedPages]; } - async addPdf(destPdf, sourcePdf) { - await mergedPdf.copyPages(pdfA, pdfA.getPageIndices()); - copiedPagesA.forEach((page) => mergedPdf.addPage(page)) - } + async collatePages(pdfA, pdfB) { + const mergedPdf = await PDFDocument.create(); + const pageCount = Math.max(pdfA.getPageCount(), pdfB.getPageCount()); + for (let i = 0; i < pageCount; i++) { + const pageA = pdfA.getPage(i); + if (pageA) mergedPdf.addPage((await mergedPdf.copyPages(pdfA, [i]))[0]); + + const pageB = pdfB.getPage(i); + if (pageB) mergedPdf.addPage((await mergedPdf.copyPages(pdfB, [i]))[0]); + } + return mergedPdf; + } /** * Part of the Classic (non-Wacky) flow. Called by [createsignatures]. * (conditionally) populates the destPdf and (conditionally) generates the outname PDF * * @param {Object} config - object /w the following parameters: - * @param {string|null} config.outname : name of pdf added to ongoing zip file. Ex: 'signature1duplex.pdf' (or null if no signature file needed) * @param {PageInfo[]} config.pageList : objects that contain 3 values: { isSigStart: boolean, isSigEnd: boolean, info: either the page number or 'b'} * @param {boolean} config.back : is 'back' of page (boolean) * @param {boolean} config.alt : alternate pages (boolean) - * @param config.destPdf : PDF to write to, in addition to PDF created w/ `outname` (or null) - * @param config.providedPages : pages already embedded in the `destPdf` to assemble in addition (or null) * @return reference to the new PDF created */ async writepages(config) { @@ -600,24 +606,21 @@ export class Book { * PDF builder base function for Classic (non-Wacky) layouts. Called by [createoutputfiles] * * @param {Object} config - * @param {PageInfo[][]|PageInfo[]} config.pageIndexDetails : a nested list of objects. - * @param config.embeddedPages : list of lists of embedded pages from source document ( [0] for duplex & front, [1] for backs -- value is null if no aggregate printing enabled) - * @param {string} config.id : string dentifier for signature file name (null if no signature files to be generated) - * @param {string[]} config.fileList : list of filenames for sig filename to be added to (modifies list) + * @param {PageInfo[][]} config.pageIndexDetails : a nested list of objects. */ async createSignatures(config) { const pages = config.pageIndexDetails; - const pdfFront = await this.writepages({ - pageList: pages[0], - back: false, - alt: false, - }); - const pdfBack = await this.writepages({ - pageList: pages[1], - back: true, - alt: false, - }); - return [pdfFront, pdfBack]; + const pdfFront = await this.writepages({ + pageList: pages[0], + back: false, + alt: false, + }); + const pdfBack = await this.writepages({ + pageList: pages[1], + back: true, + alt: false, + }); + return [pdfFront, pdfBack]; } bundleSettings() { @@ -626,7 +629,7 @@ export class Book { `Imposer settings: ${JSON.stringify(currentConfig, null, 2)}` + '\n\n' + `Link to the imposer with these settings: ${window.location.href}`; - this.zip?.file('settings.txt', settings); + this.zip.file('settings.txt', settings); } saveZip() {