Search code examples
angular6export-to-pdfangular-material-6

How do I "save as pdf" mat-table in angular 6


I have used angular-materialize theme. I am beginner for Angular framework. But I don't find a right way to export table as pdf.

I have created demo sample from https://material.angular.io/components/table/overview this official site.

My requirement is select date and then allow user to print in pdf. if anyone has an idea or example, It would be very helpful.

Demo: https://stackblitz.com/angular/xbprlqrqjyq?file=app%2Ftable-basic-example.html


Solution

  • HTML:

    Blockquote

      <button mat-button color="accent" (click)="print()">
       <mat-icon class="mat-24" aria-label="Example icon-button with a heart icon">print</mat-icon>
    Print
    

    Blockquote

    Javascript/typescript:

    import jsPDF from 'jspdf';
    import 'jspdf-autotable';
    print = () => {
      let doc = new jsPDF(); 
      doc.autoTable({
        head: [['Log','', 'Amount']],
        body: this.getLiveData() //returning [["log1", "$100"], ["log2", "$200"]]
      });
      doc.save('table.pdf')
    }