One way to convert HTML to PDF is by using jsPDF library.
For a detailed example, you can visit:
To get started, you need to install jspdf and html2canvas:
npm install jspdf
npm install html2canvas
After installation, make sure to import them where you intend to use them:
import * as jspdf from 'jspdf';
import html2canvas from 'html2canvas';
Next, update your component.html with the necessary elements:
<div id="content" #content>
<mat-card>
<div class="alert alert-info">
<strong>Html To PDF Conversion - Angular 6</strong>
</div>
...
</mat-card>
...
</div>
Finally, update your component.ts file to handle the conversion process:
import { Component, OnInit, ElementRef ,ViewChild} from '@angular/core';
import * as jspdf from 'jspdf';
import html2canvas from 'html2canvas';
@Component({
selector: 'app-htmltopdf',
templateUrl: './htmltopdf.component.html',
styleUrls: ['./htmltopdf.component.css']
})
export class HtmltopdfComponent{
public captureScreen()
{
var data = document.getElementById('contentToConvert');
html2canvas(data).then(canvas => {
// Few necessary setting options
...
});
}
}
With these steps, you should now be able to convert HTML content to PDF using Angular 6 and jsPDF. Good luck!