![]() |
VOOZH | about |
Exporting data in Angular to PDF is a common requirement for applications that handle reports, invoices or downloadable content, in this article, we'll explain how to manually export data from an Angular application to PDF using jsPDF, a popular JavaScript library for creating PDF documents.
If you do not have Angular CLI installed globally then run the following command in PowerShell or your command line:
npm install -g @angular/cliThis installs Angular CLI globally making ng command available system-wide.
When the installation is complete Verify that Angular CLI is installed correctly by running this:
ng --versionIf Angular CLI is installed correctly, you should view the Angular CLI version details and associated dependencies.
ng new angular-pdf-export-gfg
cd angular-pdf-export-gfg
It will create a basic angular project.
npm install jspdf html2canvas --save"dependencies": {
"@angular/animations": "^18.2.0",
"@angular/common": "^18.2.0",
"@angular/compiler": "^18.2.0",
"@angular/core": "^18.2.0",
"@angular/forms": "^18.2.0",
"@angular/platform-browser": "^18.2.0",
"@angular/platform-browser-dynamic": "^18.2.0",
"@angular/router": "^18.2.0",
"html2canvas": "^1.4.1",
"jspdf": "^2.5.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.10"
}
It will add a simple HTML template with button to trigger PDF creation and a section with content that we will export as PDF.
In the component’s TypeScript file, we'll import the jsPDF and html2canvas libraries and write the logic for generating the PDF.
To make table look decent in the PDF add some basic styles in app.component.css:
ng serveOpen http://localhost:4200/ in your browser. You should see a simple page with content and a button that lets you export the data to PDF, when you click the button, the content (including tables) will be exported and downloaded as PDF.
After this tutorial You have successfully implemented the functionality to export data to PDF in Angular using jsPDF and html2canvas, this approach works with static content as well as dynamically generated content, this makes it highly flexible for use in real-world applications, you can now extend this functionality to include charts, graphs, and other elements in your PDFs.