【问题标题】:pdfmake generates blank pdfpdfmake 生成空白 pdf
【发布时间】:2018-01-02 16:35:23
【问题描述】:

我正在尝试使用pdfmake 从带有html2canvas in angular2 的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 不是仅在 pdf 中打印 data 来自 docDefinition 正在打印,其他 2 个正在打印空白。

我错过了什么?

【问题讨论】:

    标签: angular html2canvas pdfmake


    【解决方案1】:

    使用下面的代码而不是使用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 而不是空白页

    【讨论】: