Transform HTML content into a PDF document with page breaks

Currently, I am developing a function that involves an HTML template.

The purpose of this function is to generate a dynamic template and convert it into a PDF.

So far, I have been able to achieve this using the following code:

var output = '';
async.each(input.contentInfo, function(component, next) {
    renderTemplate(component, function(err, result){ //compile input and template together
        output = output + result; //merge all component's HTML source code together
        next();
    });
}, function(err) {
    conversion({        //phantom-html-to-pdf function from npm module
        html: output,
        paperSize: {
            format: 'A8', 
            orientation: 'landscape',
            margin: 0,
            headerHeight: 0,
            footerHeight: 0
        }
    }, function(err, pdf) {
        var outputFile = fs.createWriteStream('output.pdf');
        pdf.stream.pipe(outputFile);
    });
});

Without error handling in this example, let's assume everything functions correctly here.

After running this function, the output variable contains content similar to this:

<div style="page-break-before: always;">
    <div style="position:absolute;left:100;top:100;>
        Page 1 Content
    </div>
</div>
<div style="page-break-before: always;">
    <div style="position:absolute;left:100;top:100;>
        Page 2 Content
    </div>
</div>

When converting this HTML to PDF, I expected it to result in a 2-page document due to the page-break CSS property. However, with the position: absolute CSS included, it does not generate multiple pages as intended. Removing the position: absolute CSS fixes the page creation issue but affects the UI layout negatively.

Is there a solution to meet both requirements in this scenario?

Any assistance on this matter would be greatly appreciated. Thank you for your help and apologies if my explanation was unclear.

Answer №1

Upon navigating the npm module documentation, I was led to this specific page. One interesting section on "Page Breaks" caught my attention:

Within CSS, there are styles such as page-break-before which can be utilized to designate HTML page breaks. This feature can also be applied with phantom-pdf in order to indicate page breaks within PDF files.

    <h1>Greetings from Page 1</h1>

    <div style='page-break-before: always;'></div>

    <h1>Greetings from Page 2</h1>

    <div style="page-break-before: always;"></div>

    <h1>Greetings from Page 3</h1>

In brief: consider experimenting with page-break-before: always;.

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

The function res.headers is not available for use, therefore jwt.sign cannot be utilized in this

I am encountering an error (res.headers is not a function) with the following code when using jwt.sign. Any ideas on how to resolve this issue? Could it be related to syntax? app.post('/logIn', async (req, res) => { //Checking if email exist ...

Displaying Name Values in a <v-select> Component and Passing the Corresponding ID in the Axios POST Request in Vue.js

UPDATE: Issue resolved by including the return-object prop in v-select When a new student is added to the database using a vuetify form, I want to be able to assign them a course that exists within a list of available courses (also stored in the database) ...

What is the best way to iterate through a collection of two or more arrays in order to determine the total length of all

https://i.stack.imgur.com/PpFlB.pngI currently have multiple Arrays containing various inputs this.listNumber = [ { "GenericQuestions": [ { "input": "long", }, { "input": & ...

Is it simple to customize the color of the close button in Bootstrap 5?

Bootstrap's v5 release brings updated styling to various components, including the close button. In previous versions, the close button had an "x" that could be easily styled with text properties, like inheriting color from its parent. However, in v5, ...

Vue Component Update DisorderI hope this meets your expectations

Below is my code using Bootstrap, Vue, and Axios: SETUP: *Please disregard the tab-contents in component_a main.js Vue.component('component_a', { props: ['info'], template: `<div> // Component A template code here } ...

Executing a click event on an HTML table dynamically created from an AJAX request

I have a script that creates an html table displaying Users of the application, with options to either EDIT or DEACTIVATE each user. These actions are handled using AJAX. The table structure is as follows: <tr><td><a id='edit_link&apo ...

The background is set and the vertical scrolling overflow is enabled

My website has an image set as the background, with a fixed position and overflow-y to allow scrolling. However, I have encountered an issue where the height of the image is causing problems on certain displays and resolutions. To temporarily fix this pro ...

What is the reason behind the absence of the C++ syntax for a string loop in javascript?

UPDATE: At first, I believed that the following syntax was not functioning due to a mistake in my code and the fact that I have not encountered it being utilized in Javascript before. My primary concern lies in why this particular syntax is not more preval ...

Three image resources combined to create a unique border design in CSS

I am currently working on designing a webpage and am in need of creating a background border. I have access to three image resources for this task: one for the left side, another for the top, and the third for the right side. My struggle lies in finding s ...

Uploading data through Ajax and integrating it into the database

I am in the process of creating a form to add a new team to our database. The goal is to insert all relevant team information into the database and simultaneously upload the team's logo to the appropriate directory within our system. <form ...

Maximizing AWS S3 for npm registration in nodeJs AWS Lambda

I have set up multiple repositories in my .npmrc file using the @scope feature: registry=https://registry.npmjs.org @my-npm-s3:registry=https://my-npm-registry.s3-eu-west-1.amazonaws.com In my package.json file, I include my @scope for a personal dependen ...

Occasionally, the 'req.user' property in Passport may unexpectedly become undefined

Currently, I am utilizing Passport for authorization and storing sessions in MongoDB using the 'connect-mongo-session' module. One issue I have encountered is that sometimes, when attempting to log in after some time has passed, the req.user obje ...

Include the data-title attribute within the <td> elements of my datatables

To apply the magic of jQuery datatables to all my tables, I use datatables. For responsive tables, I add data-title to my td's. Is there a way to automatically add data-title to all my td's like below? <td data-title="Fruit">Apple</td&g ...

Creating a new vertex in AWS Neptune database using JavaScript and Gremlin query language while optionally adding an edge

At times, it may be necessary to create a Vertex with an optional Edge. g.addV('label') .property(id, 'uniq_id_2').as('u') .property('edge_is_needed', edgeIsNeeded) .constant(edgeIsNeeded) .choose(eq(true), addE( ...

"Uncaught ReferenceError: $ is not defined - $function()" error in JavaScript/jQuery

When attempting to execute a JavaScript/jQuery function, an error is encountered when using Firebug: $ is not defined $(function()". The issue arises from the placement of the JavaScript code within a file named core.js that is referenced by index.php. W ...

Flip an image by analyzing the colors beneath it

Looking for a way to invert the color of specific areas under a mask (PNG) for a floating menu. Rather than inverting all at once, I want only certain parts to be inverted underneath the mask. Is this achievable, or is there another approach I should consi ...

Exploration of jsTree capabilities

I am currently exploring the jsTree plugin API and I'm struggling to grasp where exactly the API functions like set_theme, show_dots, etc. should be applied. On this page, I noticed that some functions are preceded by jQuery, while others are precede ...

Should we pause JQUERY AJAX to prioritize usability, or only when absolutely necessary?

I am struggling with my LoadingStatus Function, which has two options: SHOW and HIDE. When a JQUERY POST is made, the Show option triggers to display, while the HIDE option occurs after the RESPONSE comes back. The problem I'm encountering is that s ...

Routing in Next.js to create custom URL slugs for usernames, like (site.com/username), is a

I have a requirement to create username pages on my website, where each username will have its own page like site.com/jack The current folder structure I am using is pages > [user] > index.js, but this setup causes issues when someone tries to acces ...

SequelizeDatabaseError: Unselected database error encountered

I'm currently utilizing sequelize within my Express.js application, connected to a MySQL database. All of the necessary database credentials are securely stored in a .env file and have been verified to be accurate: DATABASE_NAME= mydb DATABASE_USERN ...