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 is the best way to establish a minimum width in pixels and a maximum width determined by the variable width of the browser window?

In this scenario: http://jsbin.com/anoyik/edit#preview http://jsbin.com/anoyik/edit#source Is there a way to make the red box's width a minimum of 200px and allow it to expand as the browser window's width increases horizontally? ...

Experiencing a problem with the localhost connection

I've been trying to work on this issue using React and local host, but it keeps showing the direct file instead of the website. I've been stuck on this problem for the past 3-4 hours and still haven't been able to find a solution. I'm h ...

AngularJS - Creating a dynamic wizard experience using UI-Router

I have set up my routes using ui-router in the following way: $stateProvider .state('root', { url: "", templateUrl: 'path/login.html', controller: 'LoginController' }) .state('basket&a ...

Error when attempting to open and close a div through a click event

Currently, I am in the process of developing a web service that generates arrays. I am facing an issue where all the div elements on my page load open by default, but I want them to remain closed and only open upon clicking. Specifically, I want a nested d ...

Is there a way to apply CSS styles to a specific word within a div that corresponds to the word entered in the search box?

Using a searchbox, you can enter words to find pages with the searched-for word in the page description. If the word is found in the description, a link to the page along with the highlighted word will be displayed. However, I am encountering an issue wher ...

Can you guide me on implementing CSS Houdini in Next.js?

Exploring the world of CSS Houdini in my Next.js project has been quite an adventure. After successfully installing the necessary CSS Houdini package and css-paint-polyfill using yarn, I decided to follow the webpack guidelines provided at . Below is a sn ...

Concealing a Class on the Homepage in Joomla 3

Is there a way to only hide a certain class on the home page? <div class="search-wrapper"> .search-wrapper { background: none repeat scroll 0 0 #3d4895; height: 50px; margin-top: 10px; width: 100%; } I want this class to be visible ...

Showcased a few items from the model.forms dataset

My table has three fields: id, name, status. When I use {{ form|bootstrap }}, all fields are displayed. The 'name' field is a drop-down in HTML. I only want to show entries where the status is equal to 1. How can I achieve this? ...

Retry an Ajax request immediately after a timeout without having to wait for the full

I am attempting to resend an AJAX request every 5 seconds if there is an issue, but when I simulate an offline connection with Chrome, the AJAX request doesn't wait 5 seconds between each attempt and keeps getting called continuously. What could be c ...

Mobile values in tailwindcss take precedence over breakpoints values, overriding them

I have encountered a puzzling issue that seems to have no answers thus far. Whenever I set paddings or margins for mobile devices and attempt to adjust these values for larger screens, the original mobile base value stubbornly persists and overrides my sp ...

change visibility:hidden to visible in a css class using JavaScript

I've put together a list of Game of Thrones characters who might meet their demise (no spoilers included). However, I'm struggling with removing a CSS class as part of my task. Simply deleting the CSS is not the solution I am looking for. I' ...

Explore registrations by year and month

I am currently working on coding where a record needs to be displayed based on the date (Month, Year), for instance: 12-2022. However, with the current code I have, I am unable to achieve this. Can anyone offer some guidance on what steps I should take? ...

Achieving left alignment for Material-UI Radio buttons: Float them left

Click here to view the demo https://i.stack.imgur.com/Yt4ya.png Check out the demo above to see the code in action. I'm currently struggling to align the radio buttons horizontally, wondering if there's an easier way to achieve this using Mater ...

What is the best way to hide or eliminate spinners/arrows in react-select?

I am currently utilizing react-select for my project, but I'm encountering an issue with removing the spinners/arrows from the dropdown menu. So far, I have successfully removed the default separator "|" and Dropdown Indicator using the following cod ...

Tips on revealing the following div using jQuery

Can anyone help me figure out how to reveal the next div with a class of .hide when a Clickbox is clicked? Here is a link to the demo: http://jsfiddle.net/fvuqW/ This is the jQuery code I currently have: $(function(){ $('.form input[type=checkb ...

Using Discord.js to filter messages that start with a specific phrase and finding a solution for handling DiscordAPIError when using bulk

This is the code I am currently using. Shout out to @André Dion for the assistance. if (message.channel.type == 'text') { message.channel.fetchMessages().then(messages => { const botMessages = messages.filter(msg => msg.auth ...

Crafting Effective AngularJS Directives

Recently, I've been delving into AngularJS and have grasped the core concepts quite well. As I embark on building my own application, I find myself struggling with laying out the blueprint, particularly in terms of directive design. Are there any not ...

Tips for avoiding parent div click interference in Angular

Working with Angular8, I have a div containing a routelink along with other components including a checkbox. Here's the structure: <div [routerLink]="['/somewhere', blablabla]"> <!--other components that navigate to the ro ...

Issue encountered: Inoperable binding when employing ko.mapping with two distinct models

I've been struggling to implement a drop-down select in conjunction with a table on a page using knockout bindings. The issue arises when I try to use the mapping options in the knockout binding plugin – either the drop-down or the table behaves inc ...

Trouble with FilterBy Feature in Bootstrap-Table

I am attempting to use filtering functionality with a table that is populated via JSON, utilizing bootstrap-table by wenzhixin. HTML snippet: <button class="btn btn-primary" id="filterBtn">Filter</button> <div class="container"> &l ...