pdfmake生成空白pdf [英] pdfmake generates blank pdf

查看:108
本文介绍了pdfmake生成空白pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pdfmakeangular2中的html2canvas的HTML生成pdf 这是我的代码,这是一个示例代码,我试图做原始的html包含来自API的动态数据,并且整个页面太长,这就是为什么我尝试用div进行划分的原因

I'm trying to use pdfmake for generating pdf from html with html2canvas in angular2 here is my code this is a sample code what im trying to do original html contains dynamic data from APIs and whole page is too long thats why I tried to divide it with div

component.ts

createPdf(): void {
        var data_1;
        var data_2;
        var data_3;
        var data_4;
        var data;
     var p1 = new Promise((resolve, reject) => { 
        html2canvas(document.getElementById('home'), {
                onrendered: function (canvas) {
                  console.log(canvas);
                  canvas.height = 500;
                  data_1 = canvas.toDataURL();
                    resolve(data_1);
                }
            }); 
      });  

     var p2 = new Promise((resolve, reject) => { 
         var div = document.getElementById('terms');
        html2canvas(div, {
                onrendered: function (canvas) {
                canvas.height = div.clientHeight;
                  data_2 = canvas.toDataURL();
                    resolve(data_2);
                }
            }); 
      });
     Promise.all([p1, p2]).then(values => { 
          html2canvas(document.getElementById('optionals'), {
            onrendered: function (canvas) {
                canvas.height = 500;
                var data = canvas.toDataURL();
                console.log(canvas);
                 var docDefinition = {
                    content: [
                        {
                            image: data_1,
                            width: 450,
                        },
                        {
                            image:data_2,
                            width: 500
                        },
                        {
                            image:data,
                            width:550
                        }
                        ]
                };
                console.log(docDefinition);
                setTimeout(() => {
                pdfMake.createPdf(docDefinition).download("travel.pdf");
               },1500);
            },
            removeContainer:true
        },


        );
      }, reason => {
        console.log(reason)
      });
    }

component.html

<a href="javascript:void(0)" (click)="createPdf()">
   <div class="quotation-info">
      <i class="fa fa-download"></i>

   </div>
</a>
<div class="row">
<div class="col-md-12">

<div id="optionals">
   Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

   Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
   <img src="https://images.pexels.com/photos/47730/the-ball-stadion-football-the-pitch-47730.jpeg" class="img-responsive">
   The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
</div>
<div id="home">
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
   <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
<div id="terms">
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
   <p> It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
   <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
   <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
   <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

</div>
</div>
</div>

data_1data_2不在pdf中打印,只有datadocDefinition正在打印其他2正在打印空白.

data_1 and data_2 is not printing in pdf only data from the docDefinition is printing other 2 are printing blank.

我缺少什么?

推荐答案

在下面的代码中使用,而不是使用onrendered方法

Used below code instead of using onrendered method

var p1 = new Promise((resolve, reject) => { 
    var inclusions = document.getElementById('inclusions');
    html2canvas(inclusions).then(function(canvas) {
        inclusions.appendChild(canvas);
        data_1 = canvas.toDataURL();
        resolve(data_1);
        console.log(inclusions);
    }); 
});

这有助于生成pdf而不是空白页

this helps to generate pdf instead blank page

这篇关于pdfmake生成空白pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆