pdfmake生成空白pdf

时间:2017-07-27 05:59:25

标签: angular html2canvas pdfmake

我正在尝试使用pdfmake通过html2canvasangular2的html生成pdf 这里是我的代码这是一个示例代码,我试图做的原始html包含来自API的动态数据,整个页面太长了,这就是为什么我试图用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不会以data格式打印docDefinition正在打印其他2打印空白。

我缺少什么?

1 个答案:

答案 0 :(得分:0)

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

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而不是空白页