Search code examples
angularhtml2canvaspdfmake

pdfmake generates blank pdf


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_1 and data_2 is not printing in pdf only data from the docDefinition is printing other 2 are printing blank.

What I'm missing?


Solution

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

    this helps to generate pdf instead blank page