Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a pdf file with graphs/chart using Angular2?

Tags:

angular

I need to create a pdf file that contains graphs/charts and download it from the angular page. I tried jspdf library, it works but it does not render the charts. I use chartJS for creating the charts on the page.

I have an idea to generate an image of the HTML page then put that generated image on the pdf file. But I think there is a better way to solve this problem.

How can I solve this?

like image 839
Ronald Abellano Avatar asked Oct 17 '25 11:10

Ronald Abellano


1 Answers

This is what we did in our application. used html2canvas for converting the portion of page into a canvas so we won't miss any style applied to the contents of the page. then covert the canvas to pdf. You can use this approach.

import * as jsPDF from 'jspdf';
import html2canvas from 'html2canvas';

htmltoPDF()
{
       // printDiv is the html element which has to be converted to PDF
      html2canvas(document.querySelector("#printDiv")).then(canvas => {
      var pdfFile = new jsPDF('p', 'pt', [canvas.width, canvas.height]);
      var imgData  = canvas.toDataURL("image/jpeg", 1.0);
      pdfFile.addImage(imgData,0,0,canvas.width, canvas.height);
      pdfFile.save('sample.pdf');

  });

}
like image 55
Sayooj V R Avatar answered Oct 20 '25 03:10

Sayooj V R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!