invoice-craft
Version:
Customizable, browser-first invoice PDF generator library with modern TypeScript API
71 lines (70 loc) • 3.35 kB
JavaScript
import { defaultValidate } from "./core/validation";
import { buildPdf } from "./pdf/pdfmake";
import { defaultFilename } from "./utils/filename";
import { pluginManager } from "./plugins/pluginSystem";
import { validateInvoice } from "./validation/advancedValidator";
export async function generateInvoicePdf(invoice, options = {}) {
let processedInvoice = invoice;
// Execute plugin hooks
if (options.plugins && options.plugins.length > 0) {
// Register temporary plugins
const tempPlugins = options.plugins;
tempPlugins.forEach(plugin => {
try {
pluginManager.register(plugin);
}
catch (error) {
console.warn(`Failed to register plugin '${plugin.name}':`, error);
}
});
// Execute before validation hooks
processedInvoice = await pluginManager.executeBeforeValidation(processedInvoice);
}
// Validation
if (options.validate) {
const validationResult = typeof options.validate === 'function'
? options.validate(processedInvoice)
: validateInvoice(processedInvoice);
if (validationResult && typeof validationResult === 'object' && 'isValid' in validationResult) {
if (!validationResult.isValid) {
throw new Error(`Validation failed: ${validationResult.errors.map(e => e.message).join(', ')}`);
}
}
}
else {
defaultValidate(processedInvoice);
}
// Execute before render hooks
if (options.plugins && options.plugins.length > 0) {
processedInvoice = await pluginManager.executeBeforeRender(processedInvoice);
}
let blob = await buildPdf(processedInvoice, options);
// Execute after render hooks
if (options.plugins && options.plugins.length > 0) {
blob = await pluginManager.executeAfterRender(blob);
}
const filename = options.filenameTemplate
? options.filenameTemplate({ invoice: processedInvoice })
: defaultFilename({ invoice: processedInvoice });
// Clean up temporary plugins
if (options.plugins && options.plugins.length > 0) {
options.plugins.forEach(plugin => {
pluginManager.unregister(plugin.name);
});
}
return { blob, filename };
}
// Export all types
export * from "./core/types";
// Export validation functions
export { validateInvoice, validateInvoiceStrict, getValidationSummary } from "./validation/advancedValidator";
// Export HTML preview
export { generatePreviewHTML } from "./preview/htmlGenerator";
// Export template system
export { createTemplate, renderTemplate, modernTemplate, minimalTemplate, creativeTemplate, availableTemplates, getTemplateById, getAllTemplates } from "./templates/templateBuilder";
// Export export formats
export { exportInvoice, downloadExport, previewExport, exportMultipleInvoices, exportToZip, getSupportedFormats, getFormatInfo } from "./export/exportFormats";
// Export batch processing
export { generateBatchInvoices, exportBatchInvoices, BatchProcessor, ProgressTracker, validateBatchInput, createTestBatch } from "./batch/batchProcessor";
// Export plugin system
export { pluginManager, createPlugin, builtInPlugins, registerBuiltInPlugins, createPluginChain, validatePlugin, getPluginInfo } from "./plugins/pluginSystem";