1.0.6 • Published 1 year ago
pdf-worker-processor v1.0.6
pdf-worker-processor
A PDF generator that uses Web Workers to handle the PDF generation process efficiently without blocking the main thread. This package is designed to export large datasets or complex document structures in PDF format while keeping your application's UI responsive.
Key Features
- Web Worker-powered PDF Generation: Offload the intensive task of PDF creation to a web worker, ensuring that the UI remains smooth and responsive, even for large data exports.
- Template-based PDF Design: Create structured and customizable PDFs using templates. The
TemplateDesignclass allows you to define headers, footers, and other sections with ease. - Transaction Report Support: Predefined transaction PDF templates, complete with headers, data rows, and footers, making it easy to generate financial or transactional reports.
- Data Chunking for Efficiency: Large datasets are automatically chunked into manageable sizes using the
chunkArrayutility, allowing for smooth PDF generation, even with huge amounts of data. - Progress Tracking: Real-time progress updates via Web Workers to keep users informed about the export status, ideal for long-running tasks.
- PDF Customization: Integrate advanced customization using
jspdfandjspdf-autotable, offering complete control over PDF structure and content. - Error Handling: Built-in error handling and worker termination support to manage errors gracefully and free resources when the task is done.
Installation
Install the package using npm:
npm install pdfworker-generatorUsage
Here’s a basic example of how to use PDFWorker-Generator to generate a PDF report:
Basic Example
Advanced Example with PDF Customization
You can customize your PDF by designing templates using the TemplateDesign class:
API Reference
Classes
PDFWorker
- constructor(updateProgress, onComplete, onError, baseUrl) : Initializes a web worker for PDF generation.
updateProgress: Function called with progress updates (percentage).onComplete: Function called when PDF generation is complete.onError: Function called when an error occurs.baseUrl: URL for worker setup.- processData(data) : Processes the provided data and starts PDF generation.
data: The transactional data to be exported to PDF, structured asTransactionPDFReportType.- terminate() : Terminates the worker once the task is completed or cancelled.
PDFGenerator
- static generate(data, existingDoc?) : Generates a PDF document based on the provided data.
data: The report data to be included in the PDF (TransactionPDFReportType).existingDoc: Optionally provide an existingjsPDFinstance to append content.
Interfaces
TransactionPDFReportType
header: Array of strings representing the header section of the report.data: 2D array where each sub-array represents a row of data in the report.footer: Array of strings representing the footer section of the report.
WorkerMessage
status: Current status of the worker (processingorcompleted).progress: Optional progress percentage.blobURL: Optional URL of the generated PDF blob.
Utilities
chunkArray(arr, chunkSize)
- Splits an array into smaller arrays (chunks) of a specified size.
arr: The array to split.chunkSize: The size of each chunk.
Dependencies
- jsPDF: A library to generate PDFs in JavaScript.
- jspdf-autotable: Plugin for
jsPDFto generate tables in PDFs.