Convert angular template.html to pdf in angular2 using jspdf

I'm new to angular 2 and I need functionality to export my html component in angular 2 to pdf using jspdf. I need to convert dynamically generated HTML which is tabular format to pdf using jspdf. example code and link to plunker:

import {Component, ElementRef, ViewChild} from "angular2/core";
declare let jsPDF; 

@Component({
  selector: 'my-app',
    template: `
     <button (click)="pdfHtml()">Download to PDF</button>
    <table #test>
        <thead >
            <th class="my-class">Name</th>
        </thead>
        <tbody>
            <tr *ngFor="#hero of heroes" >
            <td><svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg></td>
                <td>{{hero.name}}</td>
            </tr>
        </tbody>
    </table>
   
   `,
    styles: [
  `
  .my-class {
    background-color: yellow;
  }
  `
  ]
})

export class App {
  @ViewChild('test') el: ElementRef;
isClassVisible: true;
 heroes = [
    {id: 1, name:'Superman'},
    {id: 2, name:'Batman'},
    {id: 5, name:'BatGirl'},
    {id: 3, name:'Robin'},
    {id: 4, name:'Flash'},
     {id: 1, name:'Superman'},
    {id: 2, name:'Batman'},
    {id: 5, name:'BatGirl'},
    {id: 3, name:'Robin'},
    {id: 4, name:'Flash'}
];
    constructor() {
    }

    public download() {
        var doc = new jsPDF();
        doc.text(20, 20, 'Hello world!');
        doc.save('Test1.pdf');
    }
    
    public pdfHtml() {
      alert(this.el.nativeElement.innerHTML);
        let pdf = new jsPDF();
        let options = {
            pagesplit: true
        };
        pdf.addHTML(this.el.nativeElement, 0, 0, options, () => {
            pdf.save("test1.pdf");
        });
    }

}
      

Run codeHide result


Plunker example code

+3
angular typescript jspdf html-to-pdf


source to share


No one has answered this question yet

Check out similar questions:

728
Angular HTML binding
664
TypeScript Convert string to number
563
Huge amount of files generated for each Angular project
390
@Directive v / s @ Component in Angular
five
Angular2 - Create PDF from HTML using jspdf
4
How to convert HTML to pdf in angular2?
1
transmission object with common corner 2
0
jsPDF - Angular: HTML to PDF
0
How do I save a JSPDF as pdf in the Angular Assets folder?
0
Convert HTML to PDF using jspdf in angular2 without using JQUERY



All Articles
Loading...
X
Show
Funny
Dev
Pics