0

My intention is to convert a HTML page to PDF while clicking a button

I tried it by doing it like: 1.html2Canvas + jspdf

 html2canvas(data).then(canvas => { 
const contentDataURL = canvas.toDataURL('image/png');
    }

And the output I'm getting is

contentDataUrl  = data:,

and alternatively I wrote like this:

public captureScreen() { 
 var data = document.getElementById("print"); 
   html2canvas(data).then(canvas => { 
    var imgWidth = 208; 
    var pageHeight = 295; 
    var imgHeight = canvas.height * imgWidth / canvas.width; 
    var heightLeft = imgHeight; 
    const contentDataURL = canvas.toDataURL('image/png') 
    let pdf = new jspdf('p', 'mm', 'a4'); // A4 size page of PDF 
    var position = 0; 
    pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight) 
    pdf.save('MYPdf.pdf'); // Generated PDF 
  }); 
 }

I'm getting an error like:

Uncaught (in promise): Error: Supplied Data is not a valid base64-String jsPDF.convertStringToImageData
Error: Supplied Data is not a valid base64-String jsPDF.convertStringToImageData
at Object.x.convertStringToImageData

I need a valid approach to convert angular HTML page to PDF and also suggest me if any other better approaches

Note: I'm using Angular material and nested components in that html page.

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Rahul
  • 91
  • 1
  • 1
  • 4
  • There might be a dozen npm packages that do this already. Have you looked into any of them? Or, is this a learn exercise? – R. Richards Aug 02 '19 at 11:13
  • Could you give an example of the data that you are using? It is hard to understand what is causing the problem without it. – Jon Aug 02 '19 at 17:21
  • @R.Richards I tried only "export as" npm package it didn't work other than that I couldn't find any npm packages.I'll appreciate if u can suggest me any other approaches that can support angular material in converting angular page to PDF – Rahul Aug 05 '19 at 04:50

0 Answers0