Add new page while generating pdf from html with dynamic content

◇◆丶佛笑我妖孽 提交于 2019-12-08 12:00:18

问题


I'm trying to generate pdf from html with dynamic content using pdfmake. its working good but having an issue.

Html content is to large and pdf only generating one page and rest of the content from html is cut. How can I add other page to pdf if content is more than one page.

Here is my code.

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
        },
        );
    }

there was some suggestion to use pageBreakBefore but I don't know how to use it in my case.

Please suggest me a way here.


回答1:


Seems like you create an image from canvas and add this image to pdf document. Surely this image will not be broken into several parts / pages. And this huge image is cut. The only thing you can do to make it fit the page is to set the image size the page size, but you will get unreadable content. It is not what you need. And pagebreakBefore will not help you. As you cannot break one image into several parts.

What we do is creating an object with data and pass it to pdfmake. Then the content is passed to the next page automatically. Issues arise only with images when an image is rendered close to the footer or with long dynamic content and then this pageBrealBefore helps us. Not your case again.

So, you will have to read content of html elements and dynamically add it one by one to text field in that dynamic object (play with it on the pdfmake playground) and then everything will work fine.



来源:https://stackoverflow.com/questions/45096504/add-new-page-while-generating-pdf-from-html-with-dynamic-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!