Skip to content

Commit

Permalink
Supporting paper rotation (landscape) & source PDF rotations (#37)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Sithel <[email protected]>
  • Loading branch information
sithel authored May 27, 2023
1 parent a10dbf3 commit f77acea
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 45 deletions.
Binary file added docs/example_page_numbers_landscape.pdf
Binary file not shown.
30 changes: 30 additions & 0 deletions docs/example_page_numbers_landscape.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
\documentclass[showtrims]{memoir}

\usepackage{pgffor}
\usepackage[margin=0in]{geometry}

\setstocksize{4cm}{6cm}
\settrimmedsize{4cm}{6cm}{*}
%\addtolength{\textwidth}{8cm}
%\addtolength{\textheight}{8cm}

\setbinding{0cm}
\setlrmarginsandblock{0cm}{0cm}{*}
\setulmarginsandblock{0cm}{0cm}{*}

\begin{document}

\pagenumbering{gobble}

\begin{center}

\HUGE

\foreach \n in {1,...,120}{
\vspace*{1cm} \underline{\n} \newpage
}

\end{center}


\end{document}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/90ccwDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/90cwDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/bottomBindingDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/noRotationDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/topBindingDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 40 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,41 @@ <h2>File Info</h2>
<span class="row" style="padding: 10px;font-style: italic;">While some PDF viewers may display the first two pages as a spread, be aware that the first page, which is odd, will be on the right when printed. The first printed spread are pages 2 & 3 of your PDF document. Evens always on the left, odds always on the right.</span>
</div>

<div class="section" id="input">
<h2>Source Manipulation</h2>

<span class="row"><input type="radio" id="none" name="source_rotation" value="none" checked>
<label for="none">no modification</label>
<details style="padding-left:40px;"><summary> Details</summary><IMG SRC="img/noRotationDiagram.png" width="100px"/></details>
</span>
<span class="row"><input type="radio" id="90cw" name="source_rotation" value="90cw">
<label for="90cw">rotate all pages 90° clockwise</label>
<details style="padding-left:40px;"><summary> Details</summary><IMG SRC="img/90cwDiagram.png" width="100px"/></details>
</span>

<span class="row"><input type="radio" id="90ccw" name="source_rotation" value="90ccw">
<label for="90ccw">rotate all pages 90° counter clockwise (anti clockwise)</label>
<details style="padding-left:40px;"><summary> Details</summary><IMG SRC="img/90ccwDiagram.png" width="100px"/></details>
</span>
<span class="row"><input type="radio" id="out_binding" name="source_rotation" value="out_binding">
<label for="out_binding">Out binding </label>
<details style="padding-left:40px;">
<summary> Details</summary>
For books with the bound edge at the bottom of the page -- rotate all odd pages 90° clockwise, all even pages 90° anti-clockwise
<BR/>
<IMG SRC="img/bottomBindingDiagram.png" width="100px"/>
</details>
</span>
<span class="row"><input type="radio" id="in_binding" name="source_rotation" value="in_binding">
<label for="in_binding">In Binding</label>
<details style="padding-left:40px;">
<summary> Details</summary>
For books with the bound edge at the top of the page -- rotate all odd pages 90° anti-clockwise, all even pages 90° clockwise <BR>
<IMG SRC="img/topBindingDiagram.png" width="100px">
</details>
</span>
</div>

<!-- <div class="section" id="units">
<h2>Units</h2>
<span class="row"><input type="radio" id="inch" name="units" value="inch">
Expand Down Expand Up @@ -65,6 +100,11 @@ <h2>Printer</h2>
<span data-balloon-length="medium" aria-label="Duplex will alternate front and backs of sheets in a single file; Single-sided will split fronts and backs into seperate files." data-balloon-pos="up">ℹ️</span>
</span>

<span class="row">
<label for="paper_rotation_90">Rotate Paper 90° <input type="checkbox" name="paper_rotation_90"></label>
<span data-balloon-length="medium" aria-label="This puts your paper in 'landscape' -- layout still happens the same, but the porportions of things will be different" data-balloon-pos="up">ℹ️</span>
</span>

<span class="row"> <label>Alternate Page Rotation (AKA Flip on Long Side)<input type="checkbox" name="rotate_page"></label> <span id="folio_warning"><b>NOTE:</b> Not currently working for folios, sorry! Use the <a href="old/">old version</a> for now.</a></span></span>


Expand Down Expand Up @@ -275,7 +315,6 @@ <h2>Signature Info</h2>
<center>
<iframe id="pdf" style="width: 100%; height: 10px;display:none;"></iframe>
</center>

<span class="row">Total Pages: <span id="total_pages"></span></span>
<span class="row">Total Sheets: <span id="total_sheets"></span></span>
<span class="row">Number of Signatures: <span id="sig_count"></span></span>
Expand Down
82 changes: 61 additions & 21 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,23 @@ class Book {
this.duplex = false; //FIXME
this.duplexrotate = true;
this.papersize = _constants_js__WEBPACK_IMPORTED_MODULE_6__.PAGE_SIZES.A4; // default for europe
this.paper_rotation_90 = false; // make the printed page landscape [for landscaped layouts, results in portait]

// valid rotation options: [none, 90cw, 90ccw, out_binding, in_binding]
this.source_rotation = 'none'; // new feature to rotate pages on the sheets 2023/3/09
this.managedDoc = null; // original PDF with the pages rotated per source_rotation - use THIS for laying out pages

this.page_scaling = 'lockratio';
this.page_positioning = 'centered';
this.flyleaf = false;
this.spineoffset = false;
this.format = 'standardsig';
this.booksize = [null, null];
this.sigsize = 4; // preferred signature size
this.customsig = null;
this.signatureconfig = []; // signature configuration

this.input = null; // opened pdf file
this.currentdoc = null; // Itext PDFReader object
this.currentdoc = null; // uploaded PDF [Itext PDFReader object] untouched by source_rotation - use managedDoc for layout
this.pagecount = null;
this.cropbox = null;

Expand All @@ -71,7 +75,14 @@ class Book {

this.duplex = form.get('printer_type') == 'duplex';
this.duplexrotate = form.has('rotate_page');
this.paper_rotation_90 = form.has('paper_rotation_90');
this.papersize = _constants_js__WEBPACK_IMPORTED_MODULE_6__.PAGE_SIZES[form.get('paper_size')];
if (this.paper_rotation_90) {
this.papersize = [this.papersize[1], this.papersize[0]]
}

this.source_rotation = form.get("source_rotation");

this.page_scaling = form.get("page_scaling");
this.page_positioning = form.get("page_positioning");
this.flyleaf = form.has('flyleaf');
Expand All @@ -92,10 +103,8 @@ class Book {
this.signatureconfig.push(num);
}
});

}

this.booksize = [this.papersize[1] * 0.5, this.papersize[0]];
this.page_layout = form.get('pagelayout') == null ? 'folio' : _constants_js__WEBPACK_IMPORTED_MODULE_6__.PAGE_LAYOUTS[form.get('pagelayout')];
this.per_sheet = this.page_layout.per_sheet;
this.pack_pages = form.get('wacky_spacing') == 'wacky_pack';
Expand Down Expand Up @@ -148,19 +157,8 @@ class Book {

}

setbooksize(targetsize, customx, customy) {
if (targetsize == 'full') {
this.booksize = [this.papersize[1] * 0.5, this.papersize[0]]
} else if (targetsize == 'custom') {
this.booksize = [customx, customy];
} else {
this.booksize = _constants_js__WEBPACK_IMPORTED_MODULE_6__.TARGET_BOOK_SIZE[targetsize];
}

}

createpagelist() {
this.pagecount = this.currentdoc.getPageCount();
this.pagecount = (this.managedDoc == null) ? this.currentdoc.getPageCount() : this.managedDoc.getPageCount();
this.orderedpages = Array.from({ length: this.pagecount }, (x, i) => i);

if (this.flyleaf) {
Expand Down Expand Up @@ -188,8 +186,42 @@ class Book {
console.log("Calculated pagecount [",this.pagecount,"] and ordered pages: ", this.orderedpages)
}

createpages() {
async createpages() {
this.createpagelist();
this.managedDoc = await pdf_lib__WEBPACK_IMPORTED_MODULE_0__.PDFDocument.create()
var pages = this.currentdoc.getPages();
for (var i = 0; i < pages.length; ++i) {
var page = pages[i]
var embeddedPage = null
var newPage = this.managedDoc.addPage();
var rotate90cw = this.source_rotation == '90cw'
|| (this.source_rotation == 'out_binding' && i % 2 == 0)
|| (this.source_rotation == 'in_binding' && i % 2 == 1)
var rotate90ccw = this.source_rotation == '90ccw'
|| (this.source_rotation == 'out_binding' && i % 2 == 1)
|| (this.source_rotation == 'in_binding' && i % 2 == 0)

if (this.source_rotation == 'none') {
embeddedPage = await this.managedDoc.embedPage(page, undefined, [1, 0, 0, 1, 0, 0]);
newPage.setSize(embeddedPage.width, embeddedPage.height);
} else if (rotate90ccw) {
embeddedPage = await this.managedDoc.embedPage(page, undefined, [0, 1, -1, 0, page.getHeight(), 0]); // this is CCW
newPage.setSize(embeddedPage.height, embeddedPage.width);
} else if (rotate90cw) {
embeddedPage = await this.managedDoc.embedPage(page, undefined, [0, -1, 1, 0, 0, page.getWidth()]); // this is CW
newPage.setSize(embeddedPage.height, embeddedPage.width);
} else {
var e = new Error("??? what sorta' layout you think you're going to get?");
console.error(e);
throw e;
}
newPage.drawPage(embeddedPage);
embeddedPage.embed();
this.cropbox = newPage.getCropBox();
}

console.log("The updatedDoc doc has : ", this.managedDoc.getPages(), " vs --- ", this.managedDoc.getPageCount());

if (this.format == 'booklet') {
this.book = new _booklet_js__WEBPACK_IMPORTED_MODULE_3__.Booklet(this.orderedpages, this.duplex);
} else if (this.format == 'perfect') {
Expand Down Expand Up @@ -222,7 +254,8 @@ class Book {
// create a directory named after the input pdf and fill it with
// the signatures
this.zip = new (jszip__WEBPACK_IMPORTED_MODULE_8___default())();
this.filename = this.inputpdf.replace(/\s|,|\.pdf/, '');
var origFileName = this.inputpdf.replace(/\s|,|\.pdf/, '');
this.filename = origFileName

if (this.format == 'booklet') {
await this.createsignatures(this.rearrangedpages, 'booklet');
Expand All @@ -241,14 +274,18 @@ class Book {
}
}
await forLoop();

if (this.duplex && this.rearrangedpages.length > 1) {
await aggregatePdf.save().then(pdfBytes => {
// await this.managedDoc.save().then(pdfBytes => {
if (!isPreview)
this.zip.file('aggregate_book.pdf', pdfBytes);
});
}
var rotationMetaInfo = ((this.paper_rotation_90) ? "_paperRotated" : "")
+ ((this.source_rotation == 'none') ? "" : `_${this.source_rotation}`)
this.filename = `${origFileName}${rotationMetaInfo}`
resultPDF = aggregatePdf;
// // SHARKS
} else if (this.format == 'a9_3_3_4') {
resultPDF = await this.buildSheets(this.filename, this.book.a9_3_3_4_builder());
} else if (this.format == 'a10_6_10s') {
Expand Down Expand Up @@ -305,7 +342,7 @@ class Book {
}
});

let embeddedPages = await outPDF.embedPdf(this.currentdoc, filteredList);
let embeddedPages = await outPDF.embedPdf(this.managedDoc, filteredList);
blankIndices.forEach(i => embeddedPages.splice(i, 0, 'b'));

let block_start = 0;
Expand Down Expand Up @@ -701,7 +738,7 @@ class Book {
console.warn("All the pages are empty! : ",pagelist);
return;
}
let embeddedPages = await outPDF.embedPdf(this.currentdoc, filteredList);
let embeddedPages = await outPDF.embedPdf(this.managedDoc, filteredList);
// TODO : make sure the max dimen is correct here...
let papersize = isLandscape ? [this.papersize[1], this.papersize[0]] : [this.papersize[0], this.papersize[1]]
let curPage = outPDF.addPage(papersize);
Expand Down Expand Up @@ -31379,6 +31416,9 @@ function renderInfoBox(book) {
const sigArrange = document.getElementById('sig_arrange');
const totalPages = document.getElementById('total_pages');

if (book.book == null || book.book == undefined)
return

const outputPages = book.book.pagelist.reduce((acc, list) => {
list.forEach((sublist) => (acc += sublist.length));
return acc;
Expand Down
Loading

0 comments on commit f77acea

Please sign in to comment.