Skip to content

Commit

Permalink
Refactor pdf logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Cocoa committed Feb 21, 2024
1 parent fccad9c commit 025d692
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions src/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down

0 comments on commit 025d692

Please sign in to comment.