guidelines for splitting content into A4 size pages automatically using Vue

Hey there, I'm dealing with the issue of overflowing content not fitting on the next A4 page.

My situation:

  1. I'll begin with a single A4 page
  2. When the content from the previous A4 page overflows, I want the remaining content to move to the next A4 page - i.e., page 2
  3. This process must continue until reaching the Nth page

Below is a working jquery code, but I'm looking to implement it in Vue. You can find the existing solution here: https://jsfiddle.net/tk8rwnav/31/

Question: How do I apply the same solution in vue as demonstrated above with jquery?

const app = new Vue({
    el:'#app',
    data(){
      return {
        content: `<div class="A4">
  <h1>
    Title
  </h1>
  ...
   // The rest of the HTML content goes here
  ...
</div>`
      }
    }
})
.A4 {
  background: white;
  width: 21cm;
  height: 29.7cm;
  display: block;
  margin: 0 auto;
  padding: 10px 25px;
  ...
  // Additional CSS styles for A4 page layout
  ...
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>



<div id="app">
   <span v-html="content"></span>
</div>

Answer №1

One potential method to replicate the process when using v-html to display content in a template is to utilize a snip function within the mounted lifecycle hook of Vue.js to manipulate the DOM through JavaScript.

const app = new Vue({
    el:'#app',
    data(){
      return {
        max_pages: 100,
        page_count: 0,
        content: `<div class="A4">
  <h1>
    Title
  </h1>
  ...
</p>
  <p>9 Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. ...
    facilisis luctus, metus</p>
  <h1>
    Lorum lorum lorum
  </h1>
  ...
</p>
  <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis...metus</p>
  ...
  <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac ...ac dui. Donec non enim in turpis pulvinar facilisis. Ut felisl&amp;luctus, metus</p>
</div>
`
      }
    },
    mounted() {
      this.$nextTick(() => {
        const pages = document.querySelectorAll('.A4');
        pages.forEach(page => {
          this.snipMe(page);
        });
      });
    },
      methods: {
        snipMe : function(page) {
          this.page_count++;
          if (this.page_count > this.max_pages) {
            return;
          }
          var long = page.scrollHeight - Math.ceil(page.clientHeight);
          var children = [...page.children];
          
          var removed = [];
          while (long > 0 && children.length > 0) {
            var child = children.pop();
            child.parentNode.removeChild(child);
            removed.unshift(child);
            long = page.scrollHeight - Math.ceil(page.clientHeight);
          }
          if (removed.length > 0) {
            var a4 = document.createElement('div');
            a4.classList.add('A4');
            removed.forEach(oneRemoved => {
              a4.appendChild(oneRemoved);
            });
            page.parentNode.appendChild(a4);
            this.snipMe(a4);
          }
        } 
    }
})
.A4 {
  background: white;
  width: 21cm;
  height: 29.7cm;
  ...
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
   <span v-html="content"></span>
</div>

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

Unexpected issues arose with the seemingly straightforward Angular/Node application

Recently, I developed a basic application using Angular and Node which serves as a chat platform. One interesting feature is that it utilizes ng-model on a text field. However, I encountered an issue where the message being emitted by the node was consist ...

WordPress now features the ability to toggle both parent menu items when clicked, providing a more streamlined user experience

I am currently facing an issue with a menu structure, where parent menu items have submenus that need to toggle on click. However, I am encountering a problem where, upon placing the code inside a forEach loop, both submenus toggle instead of only togglin ...

Collaborating on code across AngularJS and Nodejs

Is there a more efficient way to share code between frontend and backend using javascript, specifically targeting nodejs and angularjs? The issue at hand is the repetition of enums and constant values like error codes on both sides, leading to manual copy ...

One-of-a-kind div elimination - using dynamically created divs via a jQuery event

I am using jQuery to trigger a div with live updates every 5 seconds, which is functioning properly. However, there are times when those updates need to be deleted immediately. The HTML generated by jQuery looks like this: <form> <div id="live1 ...

What is the best way to display a fresh jade view when a socket event occurs?

In my project, I am utilizing two key JavaScript files - one on the server side named server.js and another on the client side known as enterchat.js. These files are responsible for communicating via socket.io and all socket events are functioning as inten ...

The div element is just a tad smaller in size (0.085 smaller as per the CSS calculation)

I have a div with fixed height and width of 24px with a background image, but the browser is cropping off 0.1 pixels... div { width: 24px; height: 24px; background: url(svg); } Upon inspecting the computed styles, I noticed that the height is actua ...

Show information of selected item on click using jQuery

I am having trouble displaying the selected price, category, and size on the dropdown slideingDiv. The code I have tried below doesn't seem to be working as expected. I attempted to echo out the data to check if it was being sent through, but nothing ...

New to Angular: Struggling with complex form validation?

I am just starting to explore Angular and I want to tackle some more advanced input validation. My task involves a table where each row has three text inputs. I need to validate that at least one row has three non-empty input fields. If this criteria is m ...

How to Handle JSON Parsing with JavaScript for Big Numbers

Upon receiving a JSON object from an API, I encountered a problem with the value of type long. Due to Javascript's limitations in handling 64-bit numbers, when using JSON.parse on the received response, the number is rounded to the maximum value that ...

The reference to a $scope array in AngularJS has not been properly updated

Check out the code snippet below: var myapp = angular.module('myapp', []); myapp.controller('FirstCtrl', function ($scope) { $scope.people = [ { id: 1, first: 'John', last: 'Rambo' }, { id: 2, first: 'R ...

Triggering geolocation control prior to being integrated into a map on react-map-gl

import { useState, useRef, useEffect, useCallback } from "react"; import Map, { GeolocateControl, Marker } from "react-map-gl"; import "mapbox-gl/dist/mapbox-gl.css"; const App = () => { const mapRef = useRef(null); co ...

What is the best method for assigning a default value to the file input tag in HTML?

Can anyone help me with this code snippet? <input name="GG" type="file" value="< ?php echo $data['image'] ?>"> I was trying to set a default value in the form edit, but it seems to not be working. Does anyone know how to set a defau ...

Develop a custom dropdown menu using JavaScript

I've been working on creating a dropdown menu that appears after selecting an option from another dropdown menu. Here's the HTML code I'm using: <br> <select id ="select-container" onchange="addSelect('select-container') ...

The keyup event fails to trigger for the search input in datatables when ajax is being used

When loading a page with a jQuery datatable via AJAX, I am aiming to implement custom filtering when the user starts typing in the default filter provided by datatables. The custom logic needs to be applied when the keyup event is triggered. Since AJAX is ...

I'm encountering a typescript error as I migrate a Paho MQTT function from Angular 1 to Angular 2 - what could be causing this issue?

When connecting to my MQTT broker, I am working on several tasks. In my Ionic 2 and Angular 2 application, I have created an MQTT provider. Here is the provider code: import { Component } from '@angular/core'; import { NavController, ViewControl ...

The mark-compacts were unsuccessful due to reaching the heap limit, resulting in an allocation failure - the JavaScript heap ran out of memory during deployment

Whenever I try to deploy in npm, I encounter error code 134 along with a fatal error message: "Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory." I've set NODE_OPTIONS --max_old_space_size=2048, and also tr ...

Loading a chunked polyfill file in webpack only when needed - a step-by-step guide

In order to prevent unnecessary loading of polyfills, it is recommended to add some logic in the <head> section (https://webpack.js.org/guides/shimming/) <script> var modernBrowser = ( 'fetch' in window && ...

Encountered an issue trying to access undefined properties while reading 'userId' in Vue

I'm attempting to load an object using axios and display it with v-text However, an issue has arisen When I try to access an array element of the item object, it throws an exception such as Cannot read properties of undefined (reading 'userId& ...

Best practices for including jQuery in ASP.NET (or any other external JavaScript libraries)

Can you explain the distinctions among these three code samples below? Is there a preferred method over the others, and if so, why? 1.Page.ClientScript.RegisterClientScriptInclude(typeof(demo), "jQuery", Re ...

What is the typical output value that fluctuates?

I only have one input to work with. My goal is to set the input value to "5", then display "3" in the "total" field and "4" in the "total2" field. Additionally, for every increment of +1 to the input value, I want the "total" and "total2" fields to also in ...