Skip to content

Commit

Permalink
Start trying to reorganize pdf files
Browse files Browse the repository at this point in the history
  • Loading branch information
Cocoa authored and acestronautical committed Feb 22, 2024
1 parent 0f4e866 commit e34d53a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
58 changes: 48 additions & 10 deletions src/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ export class Book {
case 'customsig':
this.book = new Signatures(
this.orderedpages,
this.duplex,
this.sigsize,
this.per_sheet,
this.duplexrotate
Expand Down Expand Up @@ -343,14 +342,53 @@ export class Book {
? await this.embedPagesInNewPdf(this.managedDoc, pdf1PageNumbers)
: [null, null];
const forLoop = async () => {

for (let i = 0; i < this.rearrangedpages.length; i++) {
const signature = this.rearrangedpages[i];
await this.createsignatures({
console.log(signature);
const sigName = `${this.filename}_signature${i}`;
const [sigFront, sigBack] = await this.createSignatures({
pageIndexDetails: signature,
id: generateSignatures ? `${this.filename}_signature${i}` : null,
isDuplex: this.duplex,
fileList: this.filelist,
});

if (this.duplex) {
// collate
const sig = this.collatePages(sigFront, sigBack)

if (generateSignatures) {
await sig.save().then((pdfBytes) => {
this.zip.file(`signatures/${sigName}_duplex`, pdfBytes);
});
}

if (generateAggregate) {
const copiedPages = await aggregatePdf0.embedPages(sig, sig.getPageIndices());
copiedPages.forEach((page) => aggregatePdf0.addPage(page));
}

} else {
if (generateSignatures) {
await sigFront.save().then((pdfBytes) => {
this.zip.file(`signatures/${sigName}_side1`, pdfBytes);
});
await sigBack.save().then((pdfBytes) => {
this.zip.file(`signatures/${sigName}_side2`, pdfBytes);
});
}

if (generateAggregate) {
const copiedPagesFront = await aggregatePdf0.embedPages(sigFront, sigFront.getPageIndices());
copiedPagesFront.forEach((page) => aggregatePdf0.addPage(page));

const copiedPagesBack = await aggregatePdf1.embedPages(sigBack, sigBack.getPageIndices());
copiedPagesBack.forEach((page) => aggregatePdf1.addPage(page));
}

}

}
};
await forLoop();
Expand Down Expand Up @@ -443,6 +481,11 @@ export class Book {
return [newPdf, embeddedPages];
}

async addPdf(destPdf, sourcePdf) {
await mergedPdf.copyPages(pdfA, pdfA.getPageIndices());
copiedPagesA.forEach((page) => mergedPdf.addPage(page))
}

/**
* Part of the Classic (non-Wacky) flow. Called by [createsignatures].
* (conditionally) populates the destPdf and (conditionally) generates the outname PDF
Expand Down Expand Up @@ -583,13 +626,12 @@ export class Book {
*
* @param {Object} config
* @param {PageInfo[][]|PageInfo[]} config.pageIndexDetails : a nested list of objects.
* @param config.aggregatePdfs : list of destination PDF(s_ for aggregated content ( [0] for duplex & front, [1] for backs -- value is null if no aggregate printing enabled)
* @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 {boolean} config.isDuplex : boolean
* @param {string[]} config.fileList : list of filenames for sig filename to be added to (modifies list)
*/
async createsignatures(config) {
async createSignatures(config) {
const pages = config.pageIndexDetails;
// duplex printers print both sides of the sheet,
if (config.isDuplex) {
Expand All @@ -613,16 +655,12 @@ export class Book {
back: false,
alt: false,
});
await this.writepages({
outname: outname2,
const pdfBack = await this.writepages({
pageList: pages[1],
back: true,
alt: false,
});
config.fileList[config.index * 2] = outname1;
config.fileList[config.index * 2 + 1] = outname2;
}
console.log('After creating signatures, our filelist looks like: ', this.filelist);
return [pdfFront, pdfBack];
}

bundleSettings() {
Expand Down
5 changes: 2 additions & 3 deletions src/signatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ export class Signatures {
/**
* Create a signature.
* @param {number[]} pages - List of pages in a book.
* @param {boolean} duplex - Whether both front and back sides go in the same file or not.
* @param {number} per_sheet - number of pages per sheet (front and back combined)
* @param {boolean} duplexrotate - whether to rotate alternating sheets or not.
*/

constructor(pages, duplex, sigsize, per_sheet, duplexrotate) {
constructor(pages, sigsize, per_sheet, duplexrotate) {
this.sigsize = sigsize;
this.duplex = duplex;
this.duplex = false;
this.inputpagelist = pages;
this.per_sheet = per_sheet || 4; // pages per sheet - default is 4.
this.duplexrotate = duplexrotate || false;
Expand Down

0 comments on commit e34d53a

Please sign in to comment.