pdf-export-worker
v1.1.0
Published
This package allows you to generate and export PDF files in an efficient, non-blocking way by leveraging Web Workers. By offloading the computationally intensive PDF creation process to a separate thread, the main UI thread remains responsive, ensuring sm
Downloads
127
Maintainers
Readme
pdf-export-worker
pdf-export-worker
is a JavaScript library designed to streamline the process of generating large PDF files in the browser efficiently. By utilizing Web Workers and jsPDF, this library handles PDF creation and download without blocking the main thread, allowing for efficient memory management and smooth user experience even when processing extensive data sets.
Features
- Web Worker Integration: Offloads PDF generation to a background worker to improve performance and prevent UI blocking.
- Chunked Data Processing: Efficiently processes large datasets by splitting them into manageable chunks.
- Custom Table Design: Supports customizable table headers, body content, and footers, allowing for well-structured, stylized PDF documents.
- Memory Management: Releases resources after processing to optimize memory usage.
- Error Handling: Provides real-time feedback on PDF generation status and handles errors gracefully.
Installation
To install the pdf-export-worker library, use npm or yarn:
npm install pdf-export-worker
# or
yarn add pdf-export-worker
Usage
Basic Setup
In your main JavaScript or TypeScript file, import and call the executePDFGeneratorWorker
function to initiate PDF generation.
1. Import the library
import { executePDFGeneratorWorker } from 'pdf-export-worker';
2. Prepare your data
Define the structure of your PDF report data using the TransactionPDFReportType
interface.
import { TransactionPDFReportType } from 'pdf-export-worker';
const data: TransactionPDFReportType = { header: ['Column 1', 'Column 2', 'Column 3'], data: [ ['Row 1 Data 1', 'Row 1 Data 2', 'Row 1 Data 3'], // Add more rows as needed ], footer: ['Footer 1', 'Footer 2', 'Footer 3'], };
3. Generate and Download the PDF
Use the executePDFGeneratorWorker
function, passing in the data and the desired filename for the PDF
executePDFGeneratorWorker(data, 'SampleReport.pdf') .then(() => console.log('PDF generated and downloaded successfully!')) .catch((error) => console.error('PDF generation failed:', error));
Advanced Configuration
The pdf-export-worker library allows additional customization of the PDF layout, including custom table styling and PDF templating.
Template Customization
The library includes a TemplateDesign
class, which can be used to apply specific design templates for your PDF tables.
API Reference
executePDFGeneratorWorker(data: TransactionPDFReportType, fileName: string): Promise<void>
Initiates PDF generation in a Web Worker, handling large datasets by chunking and efficient memory management.
- Parameters :
data
: TransactionPDFReportType - Data for the PDF report, including header, body, and footer.fileName
: string - Name of the PDF file to be downloaded.- Returns :
Promise<void>
- Resolves when the PDF is successfully generated and downloaded.
generatePDF(data: TransactionPDFReportType, existingDoc?: jsPDF): ArrayBuffer
Generates a PDF document based on the provided data and optional existing jsPDF instance.
- Parameters :
data
: TransactionPDFReportType - Data for the PDF report.existingDoc
: jsPDF (optional) - Existing jsPDF document instance for appending content.- Returns :
ArrayBuffer
- PDF content in ArrayBuffer format.
TemplateDesign
Class for creating template designs to apply consistent styles and formatting to the PDF document.
Error Handling
- Status Messages : The worker communicates progress via status messages (
processing
,completed
, orerror
). - Error Messages : Any issues encountered during processing are sent to the main thread for handling and user notification.
Example
Below is an example setup that uses pdf-export-worker for generating a large PDF report with customized headers, footers, and efficient background processing:
Dependencies
- jsPDF : A JavaScript library for generating PDF documents.
- jsPDF-AutoTable : Extends jsPDF with support for adding tables with custom headers and footers.
License
This library is MIT licensed.