关于前端vuejs问题将动态HTML内容转换为pdf

发布于 2025-01-14 12:47:50 字数 1491 浏览 3 评论 0原文

我在如何将动态 html 内容生成为 pdf 时遇到了一些麻烦,并在实现过程中遇到了这些问题。

1-内容不适合整个页面并且页面上的组织结构不好 2- 当页面上有大量内容时,不会添加页面

pdf_statscc(){
            var HTML_Width          = $("#document").width();
            var HTML_Height         = $("#document").height();
            var top_left_margin     = 15;
            var PDF_Width           = HTML_Width + (top_left_margin * 2);
            var PDF_Height          = (PDF_Width * 1.5)+(top_left_margin * 2);
            var canvas_image_width  = HTML_Width;
            var canvas_image_height = HTML_Height;
            
            var totalPDFPages = Math.ceil(HTML_Height/PDF_Height)-1;
            html2canvas($("#document")[0],{allowTaint:true}).then(function(canvas) {
            canvas.getContext('2d');
            
            console.log(canvas.height+"  "+canvas.width);
            
            
            var imgData = canvas.toDataURL("image/png");
            var pdf = new jsPDF('p', 'pt',  [PDF_Width, PDF_Height]);
        

            pdf.addImage(imgData, 'PNG', top_left_margin, top_left_margin,canvas_image_width,canvas_image_height);
            
            
            for (var i = 1; i <= totalPDFPages; i++) { 
                pdf.addPage(PDF_Width, PDF_Height);
                pdf.addImage(imgData, 'PNG', top_left_margin, -(PDF_Height*i)+(top_left_margin*4),canvas_image_width,canvas_image_height);
            }
            
            pdf.save("reportingCC.pdf");
         });

最好的关注

I have some trouble how to generate dynamic html content to pdf and have these issues through the implementation.

1- Content are not fit the whole page and had bad oranization on the page
2- The page are not added when have to large content on the page

pdf_statscc(){
            var HTML_Width          = $("#document").width();
            var HTML_Height         = $("#document").height();
            var top_left_margin     = 15;
            var PDF_Width           = HTML_Width + (top_left_margin * 2);
            var PDF_Height          = (PDF_Width * 1.5)+(top_left_margin * 2);
            var canvas_image_width  = HTML_Width;
            var canvas_image_height = HTML_Height;
            
            var totalPDFPages = Math.ceil(HTML_Height/PDF_Height)-1;
            html2canvas($("#document")[0],{allowTaint:true}).then(function(canvas) {
            canvas.getContext('2d');
            
            console.log(canvas.height+"  "+canvas.width);
            
            
            var imgData = canvas.toDataURL("image/png");
            var pdf = new jsPDF('p', 'pt',  [PDF_Width, PDF_Height]);
        

            pdf.addImage(imgData, 'PNG', top_left_margin, top_left_margin,canvas_image_width,canvas_image_height);
            
            
            for (var i = 1; i <= totalPDFPages; i++) { 
                pdf.addPage(PDF_Width, PDF_Height);
                pdf.addImage(imgData, 'PNG', top_left_margin, -(PDF_Height*i)+(top_left_margin*4),canvas_image_width,canvas_image_height);
            }
            
            pdf.save("reportingCC.pdf");
         });

Best regard

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文