Angular service for exporting HTML/Table elements to multiple file formats
A powerful and flexible Angular library that enables exporting HTML content, tables, and DOM elements to various file formats including PDF, Excel, Word, images, and more. Built with TypeScript and fully compatible with Angular 21+ and Ionic/Capacitor.
- Features
- Supported Formats
- Installation
- Quick Start
- Usage Examples
- Configuration
- Format-Specific Options
- Browser Support
- Demo
- Dependencies
- Migration Guide (v1.20 β v1.21)
- Important Notes
- Contributing
- License
- π― Multiple Export Formats - Support for 10+ file formats
- π¦ Zero Configuration - Works out of the box with sensible defaults
- π¨ Customizable - Extensive options for each export format
- π Lightweight - Minimal dependencies and optimized bundle size
- π§ TypeScript - Full type safety and IntelliSense support
- β‘ Standalone-First - Modern Angular architecture (v1.21.0+)
- π SSR Compatible - Server-side rendering support with platform checks
- π± Ionic/Capacitor Ready - Works seamlessly with Ionic/Capacitor applications
- πͺ Two Export Modes - Download files or retrieve base64 content
- βΏ IE Support - Compatible with Internet Explorer (with polyfills)
| Format | Extension | Description | Table Required |
|---|---|---|---|
.pdf |
Portable Document Format | β No | |
| PNG | .png |
Image export | β No |
| Excel | .xlsx, .xls |
Microsoft Excel spreadsheet | β Yes |
| Word | .docx, .doc |
Microsoft Word document* | β No |
| CSV | .csv |
Comma-separated values | β Yes |
| Text | .txt |
Plain text file | β Yes |
| JSON | .json |
JavaScript Object Notation | β Yes |
| XML | .xml |
Extensible Markup Language | β Yes |
Note: Word document export (
.doc,.docx) requires TypeScript target configurationes2015or higher. See this issue for details.
npm install --save ngx-export-asyarn add ngx-export-aspnpm add ngx-export-as
β οΈ Breaking Change in v1.21.0:ExportAsModulehas been removed. The library now uses standalone components. See migration examples below.
Provide ExportAsService directly in your standalone component using modern inject():
import { Component, inject } from '@angular/core';
import { ExportAsService, ExportAsConfig } from 'ngx-export-as';
@Component({
selector: 'app-export',
templateUrl: './export.component.html',
standalone: true,
providers: [ExportAsService] // Provide service in component
})
export class ExportComponent {
private readonly exportAsService = inject(ExportAsService);
// Your export methods here
}For using the service across multiple components, provide it globally in app.config.ts:
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { ExportAsService } from 'ngx-export-as';
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
ExportAsService // Available throughout the app
]
};Then use it in any component:
import { Component, inject } from '@angular/core';
import { ExportAsService, ExportAsConfig } from 'ngx-export-as';
@Component({
selector: 'app-export',
templateUrl: './export.component.html',
standalone: true
// No need to provide service here - already in app.config
})
export class ExportComponent {
private readonly exportAsService = inject(ExportAsService);
}If you're still using NgModule-based architecture:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ExportAsService } from 'ngx-export-as';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule],
providers: [ExportAsService], // Add service to providers
bootstrap: [AppComponent]
})
export class AppModule { }Then inject in your component:
import { Component, inject } from '@angular/core';
import { ExportAsService, ExportAsConfig } from 'ngx-export-as';
@Component({
selector: 'app-export',
templateUrl: './export.component.html'
})
export class ExportComponent {
private readonly exportAsService = inject(ExportAsService);
}Create an HTML element with an ID to export:
<div id="contentToExport">
<h1>My Report</h1>
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
<tr>
<td>John Doe</td>
<td>30</td>
<td>New York</td>
</tr>
</table>
</div>
<button (click)="exportAsPDF()">Export as PDF</button>Implement the export method:
exportAsPDF() {
const exportConfig: ExportAsConfig = {
type: 'pdf',
elementIdOrContent: 'contentToExport'
};
this.exportAsService.save(exportConfig, 'MyReport').subscribe(() => {
console.log('PDF export started');
});
}Export and automatically download the file:
import { Component, inject } from '@angular/core';
import { ExportAsService, ExportAsConfig } from 'ngx-export-as';
@Component({
selector: 'app-export',
standalone: true,
providers: [ExportAsService]
})
export class ExportComponent {
private readonly exportAsService = inject(ExportAsService);
exportToPDF() {
const config: ExportAsConfig = {
type: 'pdf',
elementIdOrContent: 'myTableId'
};
this.exportAsService.save(config, 'my-report').subscribe(() => {
// File download started
console.log('Export completed!');
});
}
}Retrieve the exported content as base64 for further processing:
getBase64Content() {
const config: ExportAsConfig = {
type: 'png',
elementIdOrContent: 'chartElement'
};
this.exportAsService.get(config).subscribe((content: string) => {
// Use base64 content for upload, preview, etc.
console.log('Base64 content:', content);
// Example: Upload to server
this.uploadToServer(content);
// Example: Display in image tag
this.imagePreview = content;
});
}Export table data to Excel spreadsheet:
exportToExcel() {
const config: ExportAsConfig = {
type: 'xlsx',
elementIdOrContent: 'dataTable',
options: {
bookType: 'xlsx',
sheet: 'Sheet1'
}
};
this.exportAsService.save(config, 'data-export').subscribe(() => {
console.log('Excel file downloaded');
});
}Convert table data to JSON format:
exportToJSON() {
const config: ExportAsConfig = {
type: 'json',
elementIdOrContent: 'userTable'
};
// Note: JSON returns actual objects, not base64
this.exportAsService.get(config).subscribe((data: any[]) => {
console.log('JSON data:', data);
// Process JSON data
this.processData(data);
});
}<div id="reportContent">
<!-- Your content here -->
</div>
<div class="export-buttons">
<button (click)="export('pdf')">Export PDF</button>
<button (click)="export('xlsx')">Export Excel</button>
<button (click)="export('csv')">Export CSV</button>
<button (click)="export('png')">Export Image</button>
</div>export(format: 'pdf' | 'xlsx' | 'csv' | 'png') {
const config: ExportAsConfig = {
type: format,
elementIdOrContent: 'reportContent'
};
this.exportAsService.save(config, `report-${Date.now()}`).subscribe(() => {
console.log(`${format.toUpperCase()} export completed`);
});
}interface ExportAsConfig {
type: SupportedExtensions; // Required: Export format
elementIdOrContent: string; // Required: Element ID or content
download?: boolean; // Optional: Auto-download (used internally)
fileName?: string; // Optional: File name (used internally)
options?: any; // Optional: Format-specific options
}| Property | Type | Required | Description |
|---|---|---|---|
type |
SupportedExtensions |
β Yes | The export file format (pdf, png, xlsx, etc.) |
elementIdOrContent |
string |
β Yes | The HTML element ID to export |
download |
boolean |
β No | Internal flag for download mode |
fileName |
string |
β No | Internal filename (set via save() method) |
options |
any |
β No | Format-specific configuration options |
Powered by html2pdf.js:
const config: ExportAsConfig = {
type: 'pdf',
elementIdOrContent: 'content',
options: {
margin: 10,
filename: 'report.pdf',
image: { type: 'jpeg', quality: 0.98 },
html2canvas: {
scale: 2,
useCORS: true,
logging: false
},
jsPDF: {
unit: 'mm',
format: 'a4',
orientation: 'portrait'
},
// Custom PDF manipulation
pdfCallbackFn: (pdf) => {
// Add page numbers
const pageCount = pdf.internal.getNumberOfPages();
for (let i = 1; i <= pageCount; i++) {
pdf.setPage(i);
pdf.setFontSize(10);
pdf.text(`Page ${i} of ${pageCount}`,
pdf.internal.pageSize.getWidth() / 2,
pdf.internal.pageSize.getHeight() - 10,
{ align: 'center' }
);
}
}
}
};Common PDF Options:
margin: Margin size (number or object with top, right, bottom, left)filename: Output filenameimage: Image export options (type, quality)html2canvas: Canvas rendering optionsjsPDF: PDF generation options (unit, format, orientation)pdfCallbackFn: Custom callback function for PDF manipulation
Powered by html2canvas:
const config: ExportAsConfig = {
type: 'png',
elementIdOrContent: 'chart',
options: {
scale: 2, // Higher quality (2x resolution)
backgroundColor: '#ffffff', // Background color
logging: false, // Disable console logs
useCORS: true, // Enable cross-origin images
allowTaint: false, // Prevent canvas tainting
width: 1920, // Custom width
height: 1080 // Custom height
}
};Powered by SheetJS (xlsx):
const config: ExportAsConfig = {
type: 'xlsx',
elementIdOrContent: 'dataTable',
options: {
bookType: 'xlsx',
sheet: 'Sales Data',
raw: false, // Parse formatted values
dateNF: 'yyyy-mm-dd', // Date format
cellDates: true // Keep dates as date objects
}
};Powered by html-docx-js:
β οΈ Important: Requires TypeScripttarget: "es2015"or higher intsconfig.json
const config: ExportAsConfig = {
type: 'docx',
elementIdOrContent: 'document',
options: {
orientation: 'landscape', // or 'portrait'
margins: {
top: '20',
right: '20',
bottom: '20',
left: '20'
}
}
};No additional options required. CSV format automatically quotes values and handles special characters.
No additional options. Returns actual JSON objects (not base64):
this.exportAsService.get(config).subscribe((data: any[]) => {
// data is an array of objects
console.log(data); // [{ name: 'John', age: '30' }, ...]
});No additional options. Converts table to structured XML format.
- β Chrome (latest)
- β Firefox (latest)
- β Safari (latest)
- β Edge (latest)
- β Opera (latest)
For IE11 support, you need to enable polyfills:
Uncomment the required polyfills in src/polyfills.ts:
// Enable all BROWSER POLYFILLS for IE support
import 'core-js/es/symbol';
import 'core-js/es/object';
import 'core-js/es/function';
import 'core-js/es/parse-int';
import 'core-js/es/parse-float';
import 'core-js/es/number';
import 'core-js/es/math';
import 'core-js/es/string';
import 'core-js/es/date';
import 'core-js/es/array';
import 'core-js/es/regexp';
import 'core-js/es/map';
import 'core-js/es/weak-map';
import 'core-js/es/set';Create src/polyfills/typedarray.js:
// TypedArray polyfill for IE
if (!Int8Array.__proto__) {
console.log('Applying TypedArray polyfill...');
// Polyfill implementation
// See: https://github.com/inexorabletash/polyfill/blob/master/typedarray.js
}Import in polyfills.ts:
import './polyfills/typedarray.js';Clone and run the demo application:
git clone https://github.com/wnabil/ngx-export-as.git
cd ngx-export-as
npm install
ng build ngx-export-as
ng serveThen navigate to http://localhost:4200 in your browser.
This library uses the following open-source projects:
| Library | Version | Purpose | Documentation |
|---|---|---|---|
| html2canvas | ^1.4.1 | PNG image export | Docs |
| html2pdf.js | ^0.12.1 | PDF generation | Docs |
| SheetJS CE (xlsx) | 0.20.3 | Excel export | Docs |
| html-docx-js | - | Word document export | Docs |
π‘ Tip: Refer to the individual library documentation for advanced configuration options.
Version 1.21.0 removes the ExportAsModule in favor of Angular's modern standalone architecture.
- β Removed:
ExportAsModule(no longer exported from the library) - β
New: Direct service provision in components or
app.config.ts
Before (v1.20.x):
// app.module.ts
import { ExportAsModule } from 'ngx-export-as';
@NgModule({
imports: [
BrowserModule,
ExportAsModule // β No longer available
]
})
export class AppModule { }After (v1.21.0) - Option 1: Standalone Component
import { Component } from '@angular/core';
import { ExportAsService } from 'ngx-export-as';
@Component({
selector: 'app-export',
standalone: true,
providers: [ExportAsService] // β
Provide directly
})
export class ExportComponent {
private readonly exportAsService = inject(ExportAsService);
}After (v1.21.0) - Option 2: App Config
// app.config.ts
import { ExportAsService } from 'ngx-export-as';
export const appConfig: ApplicationConfig = {
providers: [ExportAsService] // β
App-wide provider
};After (v1.21.0) - Option 3: NgModule (Legacy)
// app.module.ts
import { ExportAsService } from 'ngx-export-as';
@NgModule({
imports: [BrowserModule],
providers: [ExportAsService] // β
Add to providers instead
})
export class AppModule { }-
Table Required Formats
- The following formats require a valid HTML
<table>element:- Excel (
.xlsx,.xls) - CSV (
.csv) - Text (
.txt) - JSON (
.json) - XML (
.xml)
- Excel (
- The following formats require a valid HTML
-
JSON Format Behavior
- Unlike other formats, JSON
get()method returns actual JSON objects, not base64-encoded strings - First table row is used as object keys (headers)
- Great for processing data in-memory before exporting
- Unlike other formats, JSON
-
Word Document Requirements
- DOCX/DOC export requires TypeScript compiler target
es2015or higher - Update
tsconfig.json:{ "compilerOptions": { "target": "es2015" } }
- DOCX/DOC export requires TypeScript compiler target
-
PDF Element Types
- PDF export accepts multiple input types:
- Element ID (string):
'myElementId' - HTMLElement:
document.getElementById('myElement') - Canvas: Direct canvas element
- Image: Image element or data URL
- Element ID (string):
- PDF export accepts multiple input types:
-
SSR (Server-Side Rendering)
- The library includes platform checks for SSR compatibility
- Canvas-based exports (PDF, PNG) only work in browser context
- Use base64 retrieval for Ionic/Capacitor or SSR applications
Contributions are welcome! Here's how you can help:
Found a bug or have a feature request?
- Check existing issues first
- Create a new issue with:
- Clear description
- Angular version
- Browser information
- Reproduction steps
- Code examples
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Make your changes
- Test thoroughly
- Commit with clear messages:
git commit -m "feat: add new feature" - Push to your fork:
git push origin feature/my-feature - Submit a pull request
# Clone the repository
git clone https://github.com/wnabil/ngx-export-as.git
cd ngx-export-as
# Install dependencies
npm install
# Build the library
ng build ngx-export-as
# Run the demo app
ng serve
# Run tests
npm test
# Build for production
npm run package- Email: breakersniper@gmail.com
- GitHub: @wnabil
- Issues: GitHub Issues
π§ If you don't receive a response within 2 days, please follow up on your issue.
This project is licensed under the MIT License.
MIT License
Copyright (c) 2025 Wassem Nabil
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
See LICENSE file for details.
Special thanks to all contributors who have helped improve this library:
- @jhoglin - Angular 20 support
- @ralphinevanbraak - Angular 19 support
- @AileThrowmountain - Angular 18 support
- @MuhAssar - Angular 17 & 15 support
- @enea4science - SSR fix
- @kgish - Angular 10 support
- @souradeep07 - CSV fix
- @Sreekanth2108 - PDF callback feature
And to all users who reported issues and provided feedback!
- NPM Package: ngx-export-as
- GitHub Repository: wnabil/ngx-export-as
- Issue Tracker: GitHub Issues
- Changelog: CHANGELOG.md
- Migration Guide: MIGRATION.md - v1.20.x β v1.21.0
- Author: Wassem Nabil
- Website: qapas.com
Made with β€οΈ by Wassem Nabil
If this library helped you, please consider giving it a β on GitHub!