
npm install jspdf
And to install html2canvas package, use given npm command.
npm install html2canvas
When we are done with the installation part, it’s time to import it into our component using the import statement.
import * as jspdf from 'jspdf'; import html2canvas from 'html2canvas';
Now, it’s time to design the user interface. Open app.component.html and paste the below code snippet.
</pre> <div id="content"> <div class="alert alert-info"><strong>Html To PDF Conversion - Angular 6</strong></div> <div></div> </div> <div> <table id="contentToConvert"> <tbody> <tr> <th>Column1</th> <th>Column2</th> <th>Column3</th> </tr> <tr> <td>Row 1</td> <td>Row 1</td> <td>Row 1</td> </tr> <tr> <td>Row 2</td> <td>Row 2</td> <td>Row 2</td> </tr> <tr> <td>Row 3</td> <td>Row 3</td> <td>Row 3</td> </tr> <tr> <td>Row 4</td> <td>Row 4</td> <td>Row 4</td> </tr> <tr> <td>Row 5</td> <td>Row 5</td> <td>Row 5</td> </tr> <tr> <td>Row 6</td> <td>Row 6</td> <td>Row 6</td> </tr> </tbody> </table> </div> <pre>
You can see in the above code that I have designed a simple HTML table to simplify this example. Do not confuse this with <mat-> tags, I have used Angular Material component to make my user interface look good. When we are done with our UI part, let’s run our application and see that output.

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 var imgWidth = 208; var pageHeight = 295; var imgHeight = canvas.height * imgWidth / canvas.width; var heightLeft = imgHeight; const contentDataURL = canvas.toDataURL('image/png') let pdf = new jspdf('p', 'mm', 'a4'); // A4 size page of PDF var position = 0; pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight) pdf.save('MYPdf.pdf'); // Generated PDF }); } }
new JsPDF( Orientation, // Landscape or Portrait unit, // mm, cm, in format // A2, A4 etc );
- addHTML()
- addFont()
- addPage()
- addMetaData()

Conclusion
Thanks for this post. I am using the html canvas but i am facing issues with it. Please check my issue here:
https://github.com/niklasvh/html2canvas/issues/1720
Let me know if you can suggest something. I am not sure what i am missing
LikeLike
Hi Neel,
Thanks for your article!
Is this possible to handle if the table with more than 1000 rows?
I need to print 50 or 100 rows per page.
LikeLike
yes we can generate multiple pages PDF as well
LikeLike