UNPKG

@casoon/invoice-generator

Version:

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

121 lines 5.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HeaderSection = void 0; const path_1 = __importDefault(require("path")); class HeaderSection { constructor(doc, currentY, logoPath) { this.doc = doc; this.currentY = currentY; this.logoPath = logoPath || 'logo.png'; // Standard: logo.png im Hauptverzeichnis } /** * Bereich 1: Header mit Adressen und Rechnungsdaten * DIN 5008: Sender links oben, Empfänger links unten, Rechnungsdaten rechts */ draw(invoice) { // Logo-Bereich (oben) this.drawLogoArea(); // Rechnungsdaten-Bereich (rechts) - wie im Bild this.drawInvoiceDetailsArea(invoice); // Empfänger-Adresse (links unten) - DIN 5008 this.drawRecipientAddressArea(invoice); // Trennlinie nach Header this.drawSeparatorLine(); return this.currentY; } drawInvoiceDetailsArea(invoice) { const rightX = 350; // Alle Bereiche rechts let y = this.currentY - 80; // Zurück zum Anfang // Logo ist bereits oben rechts positioniert (x=350) y += 80; // Platz für Logo // Rückfragen an (rechts, kleinere Schrift) this.doc.font('Helvetica-Bold') .fontSize(8) // Kleinere Schrift .text('Rückfragen an:', rightX, y, { continued: false }); y += 12; // Kleinerer Abstand this.doc.font('Helvetica') .fontSize(8) // Kleinere Schrift .text(invoice.sender.name, rightX, y, { continued: false }); y += 12; this.doc.text(`Tel: ${invoice.sender.contactInfo.phone}`, rightX, y, { continued: false }); y += 12; this.doc.text(`E-Mail: ${invoice.sender.contactInfo.email}`, rightX, y, { continued: false }); y += 15; // Abstand zwischen den Bereichen // Rechnungsdaten (rechts unten) this.doc.font('Helvetica') .fontSize(10) .text(`Rechnungs-Nr.: ${invoice.details.invoiceNumber}`, rightX, y, { continued: false }); y += 15; this.doc.text(`Kunden-Nr.: ${invoice.details.customerNumber}`, rightX, y, { continued: false }); y += 15; this.doc.text(`Rechnungsdatum: ${invoice.details.invoiceDate}`, rightX, y, { continued: false }); y += 15; this.doc.text(`Lieferdatum: ${invoice.details.deliveryDate}`, rightX, y, { continued: false }); y += 15; this.doc.text(`Leistungszeitraum: ${invoice.details.servicePeriod}`, rightX, y, { continued: false }); } drawLogoArea() { // Logo aus logoPath oder Fallback if (this.logoPath) { try { // Versuche verschiedene Pfad-Formate let logoToLoad = this.logoPath; // Wenn es ein relativer Pfad ist, mache ihn absolut if (!path_1.default.isAbsolute(this.logoPath)) { logoToLoad = path_1.default.join(process.cwd(), this.logoPath); } console.log('Versuche Logo zu laden von:', logoToLoad); this.doc.image(logoToLoad, 350, this.currentY, { width: 150 }); console.log('Logo erfolgreich geladen!'); } catch (error) { console.warn('Logo konnte nicht geladen werden:', error); this.drawLogoPlaceholder(); } } else { this.drawLogoPlaceholder(); } this.currentY += 80; // Platz für Logo } drawLogoPlaceholder() { // Sauberer Logo-Platzhalter (rechts) this.doc.font('Helvetica-Bold') .fontSize(16) .text('LOGO', 350, this.currentY + 30, { continued: false }); this.doc.rect(350, this.currentY, 150, 60) .stroke(); } drawRecipientAddressArea(invoice) { // Empfänger-Adresse links unten - DIN 5008 // Mindestens 45mm vom oberen Rand const recipientY = Math.max(this.currentY, 160); // 160px entspricht etwa 45mm // Rechnungssteller in einer Zeile über dem Empfänger - DIN 5008 const senderLine = [ invoice.sender.name, invoice.sender.address.street, `${invoice.sender.address.postalCode} ${invoice.sender.address.city}` ].filter(Boolean).join(' · '); this.doc.font('Helvetica') .fontSize(8) // Kleinere Schrift für Rechnungssteller .text(senderLine, 50, recipientY - 20, { continued: false }); // Rechnungsempfänger - kleinere Schrift und Zeilenhöhe this.doc.fontSize(12) // Kleinere Schrift (vorher 14) .text(invoice.recipient.name, 50, recipientY, { continued: false }); this.doc.fontSize(12) // Kleinere Schrift .text(invoice.recipient.address.street, 50, recipientY + 15, { continued: false }); // Kleinere Zeilenhöhe (vorher 20) this.doc.text(`${invoice.recipient.address.postalCode} ${invoice.recipient.address.city}`, 50, recipientY + 30, { continued: false }); // Kleinere Zeilenhöhe (vorher 40) // Aktualisiere currentY für den nächsten Bereich this.currentY = recipientY + 55; // Kleinere Gesamthöhe (vorher 70) } drawSeparatorLine() { this.currentY += 20; // Keine Trennlinie nach Header this.currentY += 20; } } exports.HeaderSection = HeaderSection; //# sourceMappingURL=header-section.js.map