Answers for "angular generate pdf from html"

0

angular generate pdf from html

npm install --save pdfmake
npm install html-to-pdfmake
npm install jspdf --save

In the component.ts:
import { Component, ViewChild, ElementRef } from '@angular/core';
  
import jsPDF from 'jspdf';
import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';
pdfMake.vfs = pdfFonts.pdfMake.vfs;
import htmlToPdfmake from 'html-to-pdfmake';
   
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'htmltopdf';
  
  @ViewChild('pdfTable') pdfTable: ElementRef;
  
  public downloadAsPDF() {
    const doc = new jsPDF();
   
    const pdfTable = this.pdfTable.nativeElement;
   
    var html = htmlToPdfmake(pdfTable.innerHTML);
     
    const documentDefinition = { content: html };
    pdfMake.createPdf(documentDefinition).open(); 
     
  }
}
in the component.html:
<div id="pdfTable" #pdfTable>
  ...
  <button class="btn btn-primary" 
          (click)="downloadAsPDF()">Export To PDF</button>
Posted by: Guest on October-13-2021

Browse Popular Code Answers by Language