Creating a PDF from HTML with dynamic content? Be sure to include a new page in the

I am working on generating a PDF from HTML with dynamic content using the pdfmake library. Everything is functioning well, but I have encountered an issue.

The HTML content is too large, and the generated PDF only displays one page, cutting off the remaining content. How can I add additional pages to the PDF if the content exceeds one page?

Below is the code snippet I am currently using:

createPdf(): void {
     var main = $('.main');
    html2canvas(this.el.nativeElement, {
            onrendered: function (canvas) {
                var data = canvas.toDataURL();                       
                var a4  =[ 595.28,  841.89];
                var docDefinition = {
                    pageSize: 'A4',
                    content: [{
                        image: data,
                        width: 500,
                        pageBreak:'after'
                    }],
                     pageBreakBefore: function(currentNode, followingNodesOnPage,
                      nodesOnNextPage, previousNodesOnPage) {                      
                       return currentNode.headlineLevel === 1 && followingNodesOnPage.length === 0;
                    }
                };
                pdfMake.createPdf(docDefinition).download("test.pdf");
            },
            imageTimeout:2000,
            removeContainer:true
        },
        );
    }

I received a suggestion to utilize the pageBreakBefore property, but I am unsure how to implement it in my situation.

If you have any solutions or advice regarding this matter, please feel free to share them with me.

Answer №1

It appears that the method involves creating an image from a canvas and inserting it into a PDF document. However, this process does not allow for the image to be split across multiple pages, resulting in the need to resize the image to fit the page size. Unfortunately, this may lead to unreadable content. Additionally, the pagebreakBefore property will not be useful in this scenario since breaking one image into parts is not possible.

An alternative approach is to create an object with data and send it to pdfmake, allowing content to flow onto the next page automatically. Challenges may arise when using images near the footer or with long dynamic content, where the pageBreakBefore feature may come in handy. However, this does not seem to be applicable to your current situation.

To address this issue, consider extracting the content of HTML elements and dynamically adding them one by one to a text field within the dynamic object. Experimenting with this approach on the pdfmake playground should yield satisfactory results.

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

Encountering the error "Cannot resolve all dependencies for CustomTranslationLoader" when executing Jest test cases in an Angular project

Attempting to create my initial test scenario for a login page, the contents of my ts file are as follows: import {Component, OnInit} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {FormBuilder, For ...

Nano frontend program

My current project consists of 5 different single-page applications (SPAs): 2 in Angular, 2 in React, and 1 in Vue.js. These SPAs are served by an integrated server that handles routing for each one. I am looking for a way to share data between these app ...

Issue encountered with Next.js 13.4 and NextAuth: A Type Error stating that 'AuthOptions' is not compatible with type 'never'

Currently, I am in the process of developing a Next.js 13.4 project and attempting to configure NextAuth using the app/router. Unfortunately, I have encountered a type error that I am struggling to troubleshoot. Below is my route.ts file: import NextAuth, ...

Paths: Integrate a variety of components along with essential information

With 3 components in hand, everything works smoothly when I run them on AppComponent: <app-sections #appSection></app-sections> <app-form #mainForm [section]="appSection.currentSection"></app-form> <app-match [bestMatchHTMLEleme ...

Exploring the Functionality of Object Spread and Rest Operators in Typescript 2.1 - encountering a glitch during implementation

Using Typescript 2.1.6 Here is a snippet of my code: registrant.reducers.ts const registrationReducers = { languageData: languageDataReducer, languageUi: languageUiReducer} app.reducers.ts import { registrationReducers } from '../registration ...

Using the as operator in TypeScript for type casting a string

function doSomething(a : any) { let b = (a as Array<any>) alert(typeof b) // displays "string" } doSomething("Hello") The alert is showing "string" instead of what I anticipated, which was something along the lines of a null value. The docu ...

Having trouble installing @angular/cli 4 on Debian?

I'm having trouble installing @angular/cli on my Debian box. I already have the latest versions of Node.js and npm installed. Interestingly, Angular4 works perfectly fine on my Windows machine where I use it daily. However, when I try to get it runnin ...

Optimizing my AngularJS bundle is pushing me towards upgrading to Angular 5

Regarding my AngularJS application, it was initially created using 'yo angular-fullstack' with JS scripting instead of TS. It is functional but experiencing performance and user experience issues. The deployment is on AWS ElasticBeanstalk nano i ...

What is the best way to include a variable or literal as a value in styled components?

When it comes to managing various use cases, I always rely on props. However, I am currently facing a challenge in changing the border color of a styled input during its focus state. Is there a way to utilize props for this specific scenario? Despite my f ...

Stop the pinch zoom functionality in Angular NativeScript WebView to prevent unwanted zooming

Currently, I am working on a Nativescript App that utilizes Angular (NG 5.1.1 / Angular 7.x). Within the app, there is a view containing a webview. @ViewChild("myWebView") webViewRef: ElementRef; <WebView class="webview" #myWebView [src]="myU ...

Error encountered: The property 'localStorage' is not found on the 'Global' type

Calling all Typescript enthusiasts! I need help with this code snippet: import * as appSettings from 'application-settings'; try { // shim the 'localStorage' API with application settings module global.localStorage = { ...

The system is unable to locate the module at 'C:UsersSanjaiAppDataRoaming pm ode_modulesprotractorinprotractor'. This error originates from internal/modules/cjs/loader.js at line 960

After running "protractor conf.js" without any issues, I decided to install protractor globally using the command "npm install -g protractor". However, after installing protractor globally, I encountered the following error message: internal/modules/cjs/lo ...

Using Angular 4: Redirecting Menu to Component with Electron

I am currently working on my first project using Angular 4 and Electron to develop a desktop application. One of the challenges I'm facing is figuring out how to redirect to a specific component when a submenu item is clicked after overriding the ele ...

Navigating Dynamically between tabs - A How-to Guide

I am working on a mat-tab Angular app where I need to dynamically generate links and transfer them to a navLinks object. Despite ensuring that the concatenation is correct, it seems like my approach is not working as expected. Here's a glimpse of what ...

Building an Angular 4 universal application using @angular/cli and integrating third-party libraries/components for compilation

While attempting to incorporate server side rendering using angular universal, I referenced a post on implementing an angular-4-universal-app-with-angular-cli and also looked at the cli-universal-demo project. However, I ran into the following issue: Upon ...

What is the syntax for creating a zip function in TypeScript?

If I am looking to create a zip function: function zip(arrays){ // assume more than 1 array is given and all arrays // share the same length const len = arrays[0].length; const toReturn = new Array(len); for (let i = 0; i < len; i+ ...

The object has been defined, but the specific property is returning as undefined

I have defined a class in the following way: export class User{ private _name:string constructor(){} get name():string{ return this._name } set name(val:string){ this._name = val } } Now, in my login code, I am ...

NGXS no longer functions properly when upgrading to Angular 9 and enabling Ivy

I am currently in the process of upgrading my Angular application to version 9. I have encountered an issue where everything runs smoothly when Ivy is disabled, but enabling Ivy causes the application's serve task to not finish correctly: Here is a s ...

What is the best way to set the minDate and maxDate of the NgbDatePicker in the main component so that the settings can be applied

Within my Angular 4 project, I have integrated Ng-bootstrap (v1.1.0) which includes multiple date pickers across different modules. I am looking to enforce a global maxDate configuration for all these instances. Below is an overview of my folder structure: ...

Oops! Looks like you forgot to provide a value for the form control named <name>. Make sure to fill

I have encountered an issue with a nested operation. The process involves removing an offer and then saving it again as a repeating one. However, the problem arises when I try to use patchValue() on the item in offerList[index] after saving the repeating ...