What steps should I follow to ensure that the content for the back page is printed on the back page of a booklet when using paged.js?

In the case of a booklet printed from folded sheets, it is interesting to note that the number on the back page will always be divisible by 4. If the content does not have a multiple of 4 pages, then the back page will end up inside the booklet.

For my current project, I am utilizing Paged.js for its excellent layout and pagination capabilities. Although it comes with a selector for handling the first page, there isn't one specifically for the last page.

I'm seeking advice on how to effectively insert blank pages at the conclusion of the document so that the final content page smoothly transitions onto the back cover of the booklet?

Answer №1

If there is anyone more skilled than me who could provide an enhanced version of this code, I would greatly appreciate it. While I believe that improvements can be made with functions like breakToken, the current setup works for my needs:

Within the unpaginated html, there exists a div labeled .back-page. The approach taken here involves checking if the page number corresponding to the back-page div is divisible by 4. If not, a large empty div is inserted.

The JavaScript

window.PagedConfig = { auto: false };

document.addEventListener("DOMContentLoaded", function () {
  class MyHandler extends Paged.Handler {
    constructor(chunker, polisher, caller) {
      super(chunker, polisher, caller);
    }

    beforeParsed(content) {}
    afterParsed(parsed) {}
    beforePageLayout(page) {}

    afterPageLayout(pageFragment, page, breakToken) {
      bumpBackPageContentToRealBackPage(pageFragment);
    }

    afterRendered(pages) {}
  }
  Paged.registerHandlers(MyHandler);

  setTimeout(() => window.PagedPolyfill.preview(), 2000); // TODO: replace this with the promise version
});

function bumpBackPageContentToRealBackPage(pageFragment) {
  let backPage = pageFragment.querySelector(".back-page");
  if (backPage) {
    let pageNumberStr = backPage.closest(".pagedjs_page").dataset.pageNumber;
    let pageNumber = parseInt(pageNumberStr, 10);
    console.log("last page is on", pageNumber);
    if (pageNumber % 4 !== 0) {
      console.log("last page needs a nudge");
      let pageDiv = document.createElement("div");
      pageDiv.classList.add("end-vacat-page");
      backPage.parentElement.insertBefore(pageDiv, backPage);
    } else {
      console.log("All is well: last page will print on the back page");
    }
  }
}

The CSS

.back-page {
  break-before: left;
}
.end-vacat-page {
  page-break-before: always;
  height: var(--pagedjs-pagebox-height);
}

This action triggers a reflow, ensuring that the back page aligns with the requirement to be displayed on a left-hand page according to one of the rules in the Paged.js polyfill.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What's the best way to include CSS and Javascript in an HTML document?

I'm still getting the hang of CSS and JavaScript. I stumbled upon a cool countdown timer that I'd like to incorporate into my website, but I'm not quite sure how to place it where I envision it. Check out the timer here CSS: html,body{mar ...

Data retrieval seems to be encountering issues in Firefox and IE9, whereas Chrome and Safari are functioning without any problems

I am using the following method function callCommentservice() { try { // Comment Service Url var getCommentServiceUrl = self.commentsServiceUrl + self.getRating + "tenantId=" + self.tenantId + "&ratedObjectTypeId=" + sel ...

Change the body selector in CssResource to apply custom styles

I have implemented a CssResource within a ClientBundle following the guidelines provided in the documentation at this link. To access the styles from my UiBinder xml, I have used ui:with as explained in the documentation available at this guide. While th ...

Checking the alignment of a label or element in the center of a webpage using javascript

I am new to JavaScript and was attempting to determine if an element is centered aligned by accessing its CSS properties/values. Can someone help me retrieve the CSS property to verify text alignment using JavaScript? ...

Retrieve the image source when clicking on multiple posts that share the same div class and button class

<script src="https://code.jquery.com/jquery-1.11.1.js"></script> <script> $(function(){ $('.like').click(function(e){ e.preventDefault(); // stop the page from reloading alert( $('.post-container&apos ...

What are the steps to bring in a module from a URL?

I have integrated a Vue.js npm package into my application. However, due to certain reasons, I now need to directly use the CDN URL. Let's assume that this is the CDN link for the library: https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-prot ...

Accessing elements from a controller in a nested ng-repeat in AngularJS

I am relatively new to the world of AngularJS and I've encountered a challenge that I can't seem to figure out. The issue lies within my nested ng-repeat view, as I have a nested comment system. Therefore, the view is built from an object structu ...

Tips for perfectly aligning all elements in a row within a card design

Hi there, I'm trying to figure out how to align the content of this card in a single row instead of stacked on top of each other. Here's the code snippet: <div class="container"> <form onSubmit={submitHandler}> ...

Is it possible to visually distinguish the selected mat-grid-tile? Particularly when they are being created dynamically

On the user interface, I have a dynamic display of mat-grid-tile within a mat-grid-list. These tiles change in number and data based on backend values. When a user clicks on a mat-grid-tile, it triggers a function that receives the tile's data. My goa ...

Exploring the .map() Method in ReactJS

Would it be feasible to integrate another Postgres database table into the current mapping displayed in this code? It would be ideal if it could be done using some sort of array function. {items.map(item => ( <tr key={item.id}& ...

Angular's UI Modal: utilizing inline template and controller functionality

I am looking to create a simple confirmation box using UI-modal, which I have used successfully for more complex modals in the past that load their template and controller from external files. However, this time I want something even simpler - just a basi ...

Efficiently utilizing CSS classes

My dilemma involves styling a textfield in two different locations on my website, each with its own parent element. The second location requires an additional margin-top that the first does not. What is a clever way to adjust the original margin-top withou ...

Getting variables from different functions in Python can be achieved by using the return

I am trying to implement a feature where I can fetch a search term from the function getRandomVideo() and then use it in a jQuery statement. For example, if I get "Beethoven" as the search term from the variable searches, I want to use it to retrieve JS ...

Exploring the history and present state of Vue lifecycle hooks

Is there a way to access previous and current data in the updated lifecycle hook in Vue, similar to React? I want to be able to scroll a list of elements to the very bottom, but for this I need: The already rendered updated DOM (to calculate the scroll) ...

Including files from different directories using PHP

I'm currently involved in a project for school. The index is functioning properly, however, when I click on 'Lab 1 Q1' located at labs/1/1.html, it does direct me to the page but my <?php include('/main-nav.php') ?> isn' ...

What is the best way to display my table?

In the index.php view, you will find my table located <table class="striped"> <thead> <tr> <th>Id</th> <th>Name</th> <th ...

How can one utilize Codemirror code folding while avoiding the use of "[ ]"?

I am looking forward to implementing Codemirror codefolding for folding only braces { and }, as well as comments. However, I am facing an issue where it also folds square brackets [ and ]. Since square brackets are usually part of one-line statements, I do ...

What are some ways we can enhance Map.get for react-router using ES6 Maps?

I recently implemented a map using new Map() to store my RR4 configuration within my application. My goal is to retrieve the values associated with /countries/:id when accessing /countries/1. routesMap.get('/countries/1') // should provide the ...

Unusual glitch involving onChange method in React JS

Encountered a peculiar bug with an autocomplete form. I implemented an onChange function for the form that can access two arguments, the "event" and the "value". The function is structured like this: handleChange(event, value){ const newElement = ...

Bluebird refuses to execute the then() function, despite the code that was functional before

I am in the process of constructing a node framework for my upcoming projects, with a focus on easy management. The framework already includes a configuration module. Recently, I implemented an error handler and made updates to the Config module to incorp ...