UNPKG

@casoon/invoice-generator

Version:

Generate PDF invoices, XRechnung XML, and ZUGFeRD PDF/A-3 from JSON data with TypeScript support

90 lines 3.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PDFGenerator = void 0; exports.generateInvoicePDF = generateInvoicePDF; exports.generateInvoicePDFToFile = generateInvoicePDFToFile; const pdfkit_1 = __importDefault(require("pdfkit")); const fs_1 = __importDefault(require("fs")); const sections_1 = require("./sections"); class PDFGenerator { constructor(options = {}) { this.currentY = 50; this.options = { template: 'default', language: 'de', includeLogo: false, logoPath: undefined, ...options }; this.doc = new pdfkit_1.default({ size: 'A4', margin: 50, compress: false, bufferPages: true, info: { Title: 'Invoice', Author: 'Invoice Generator', Subject: 'Invoice Document', Creator: 'PDFKit', Producer: 'PDFKit' } }); this.doc.font('Helvetica').fontSize(10); } async generateInvoice(invoice) { return new Promise((resolve, reject) => { const chunks = []; this.doc.on('data', (chunk) => chunks.push(chunk)); this.doc.on('end', () => resolve(Buffer.concat(chunks))); this.doc.on('error', reject); this.generateInvoiceContent(invoice); this.doc.end(); }); } async generateInvoiceToFile(invoice, outputPath) { return new Promise((resolve, reject) => { const writeStream = fs_1.default.createWriteStream(outputPath); this.doc.pipe(writeStream); writeStream.on('finish', resolve); writeStream.on('error', reject); this.doc.on('error', reject); this.generateInvoiceContent(invoice); this.doc.end(); }); } generateInvoiceContent(invoice) { // Reset Y-Position this.currentY = 50; // Bereich 1: Header mit Adressen und Rechnungsdaten const headerSection = new sections_1.HeaderSection(this.doc, this.currentY, this.options.logoPath); this.currentY = headerSection.draw(invoice); // Bereich 2: Ansprache zur Rechnung const salutationSection = new sections_1.SalutationSection(this.doc, this.currentY); this.currentY = salutationSection.draw(invoice); // Bereich 3: Tabelle mit Rechnungspositionen const itemsTableSection = new sections_1.ItemsTableSection(this.doc, this.currentY); this.currentY = itemsTableSection.draw(invoice); // Bereich 4: Grußformel const closingSection = new sections_1.ClosingSection(this.doc, this.currentY); this.currentY = closingSection.draw(invoice); // Wiederkehrender Footer const footerSection = new sections_1.FooterSection(this.doc, this.currentY); this.currentY = footerSection.draw(invoice); } } exports.PDFGenerator = PDFGenerator; /** * Convenience-Funktionen für einfache Verwendung */ async function generateInvoicePDF(invoice, options) { const generator = new PDFGenerator(options); return generator.generateInvoice(invoice); } async function generateInvoicePDFToFile(invoice, outputPath, options) { const generator = new PDFGenerator(options); return generator.generateInvoiceToFile(invoice, outputPath); } //# sourceMappingURL=pdf-generator.js.map