OfficeServer/create-invoice-template.js
dgsoft a01423321b Complete migration from docx-templates to docxtemplater
 Migration completed:
- server.js: Complete rewrite using docxtemplater + pizzip
- server-old.js: Backup of original docx-templates implementation
- package.json: Added docxtemplater and pizzip dependencies
- Removed docx-templates dependency

🚀 New features:
- True dynamic tables with {#array}{field}{/array} syntax
- Unlimited table rows (no more fixed array indices)
- New API endpoints: /analyze, /generate, /demo
- Simplified template creation scripts

🎯 Template improvements:
- Updated template syntax examples
- Added dynamic-template.docx with loop syntax
- Enhanced template analysis functionality

📊 Benefits:
- Real dynamic table generation
- Better template flexibility
- Cleaner API design
- Preserved SSL and WebDAV functionality
2025-10-01 22:14:19 +02:00

270 lines
10 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const { createReport } = require('docx-templates');
// Rechnungsvorlage erstellen
const invoiceTemplate = `
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:pPr><w:jc w:val="center"/></w:pPr>
<w:r><w:rPr><w:b/><w:sz w:val="32"/></w:rPr><w:t>RECHNUNG</w:t></w:r>
</w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:tbl>
<w:tr>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Rechnungssteller:</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Kunde:</w:t></w:r></w:p></w:tc>
</w:tr>
<w:tr>
<w:tc>
<w:p><w:r><w:t>++firma++</w:t></w:r></w:p>
<w:p><w:r><w:t>++straße++</w:t></w:r></w:p>
<w:p><w:r><w:t>++plz++ ++stadt++</w:t></w:r></w:p>
<w:p><w:r><w:t>Tel: ++telefon++</w:t></w:r></w:p>
<w:p><w:r><w:t>Email: ++email++</w:t></w:r></w:p>
</w:tc>
<w:tc>
<w:p><w:r><w:t>++kunde_name++</w:t></w:r></w:p>
<w:p><w:r><w:t>++kunde_straße++</w:t></w:r></w:p>
<w:p><w:r><w:t>++kunde_plz++ ++kunde_stadt++</w:t></w:r></w:p>
</w:tc>
</w:tr>
</w:tbl>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:tbl>
<w:tr>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Rechnungsnummer:</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++rechnungsnummer++</w:t></w:r></w:p></w:tc>
</w:tr>
<w:tr>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Rechnungsdatum:</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++datum++</w:t></w:r></w:p></w:tc>
</w:tr>
<w:tr>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Fälligkeitsdatum:</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++fälligkeitsdatum++</w:t></w:r></w:p></w:tc>
</w:tr>
</w:tbl>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Rechnungsposten:</w:t></w:r></w:p>
<w:tbl>
<w:tr>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Pos.</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Artikel/Leistung</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Menge</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Einzelpreis</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Gesamtpreis</w:t></w:r></w:p></w:tc>
</w:tr>
<w:tr>
<w:tc><w:p><w:r><w:t>++positionen[0].nr++</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++positionen[0].artikel++</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++positionen[0].menge++</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++positionen[0].einzelpreis++</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++positionen[0].gesamtpreis++</w:t></w:r></w:p></w:tc>
</w:tr>
</w:tbl>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:tbl>
<w:tr>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Nettobetrag:</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++nettobetrag++</w:t></w:r></w:p></w:tc>
</w:tr>
<w:tr>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>MwSt. (19%):</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++mwst++</w:t></w:r></w:p></w:tc>
</w:tr>
<w:tr>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Gesamtbetrag:</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++gesamtbetrag++</w:t></w:r></w:p></w:tc>
</w:tr>
</w:tbl>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:t>Vielen Dank für Ihr Vertrauen!</w:t></w:r></w:p>
<w:p><w:r><w:t>Bei Fragen erreichen Sie uns unter: ++email++</w:t></w:r></w:p>
</w:body>
</w:document>`;
// Minimales DOCX erstellen
const minimalDocx = Buffer.from([
'UEsDBAoAAAAAAHJZMlUAAAAAAAAAAAAAAQAAAFsQ=',
'b250ZW50X1R5cGVzXS54bWyLycgsyk1VKMsvyk1VKE5OzStJTVEozk8p0S8pzUlNTi0qSsxLTi2y0lEDAEVKB',
'A9QSwECCgAAAAAAclkyVQAAAAAAAAAAAAABAAoAABgAAAAAAAAAAAA==',
'[Content_Types].xml',
'UEsFBgAAAAEAAQAwAAAAOgAAAAA='
].join(''), 'base64');
// Einfaches Template erstellen mit createReport
async function createInvoiceTemplate() {
try {
const templatePath = path.join(__dirname, 'templates', 'rechnung-template.docx');
// Einfaches Template als Basis verwenden und dann überschreiben
const baseTemplate = fs.readFileSync(path.join(__dirname, 'templates', 'simple-template.docx'));
// Rechnung-Template mit erweiterten Tags erstellen
const invoiceContent = await createReport({
template: baseTemplate,
data: {
// Dummy-Daten um Template-Struktur zu erstellen
firma: '++firma++',
straße: '++straße++',
plz: '++plz++',
stadt: '++stadt++',
telefon: '++telefon++',
email: '++email++',
kunde_name: '++kunde_name++',
kunde_straße: '++kunde_straße++',
kunde_plz: '++kunde_plz++',
kunde_stadt: '++kunde_stadt++',
rechnungsnummer: '++rechnungsnummer++',
datum: '++datum++',
fälligkeitsdatum: '++fälligkeitsdatum++',
positionen: [
{
nr: '++positionen[0].nr++',
artikel: '++positionen[0].artikel++',
menge: '++positionen[0].menge++',
einzelpreis: '++positionen[0].einzelpreis++',
gesamtpreis: '++positionen[0].gesamtpreis++'
}
],
nettobetrag: '++nettobetrag++',
mwst: '++mwst++',
gesamtbetrag: '++gesamtbetrag++'
}
});
fs.writeFileSync(templatePath, invoiceContent);
console.log('✅ Rechnungs-Template erstellt:', templatePath);
} catch (error) {
console.error('❌ Fehler beim Erstellen des Rechnungs-Templates:', error);
}
}
// Da wir das base template brauchen, erstellen wir das Template manuell
const fs2 = require('fs');
const AdmZip = require('adm-zip');
function createInvoiceTemplateManual() {
try {
// Neues ZIP für DOCX erstellen
const zip = new AdmZip();
// [Content_Types].xml
const contentTypes = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
</Types>`;
// _rels/.rels
const rels = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>`;
// word/document.xml - Rechnung Template
const document = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:pPr><w:jc w:val="center"/></w:pPr>
<w:r><w:rPr><w:b/><w:sz w:val="32"/></w:rPr><w:t>RECHNUNG</w:t></w:r>
</w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:tbl>
<w:tblPr><w:tblW w:w="0" w:type="auto"/></w:tblPr>
<w:tr>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Firma: ++firma++</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Kunde: ++kunde_name++</w:t></w:r></w:p></w:tc>
</w:tr>
<w:tr>
<w:tc><w:p><w:r><w:t>++straße++</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++kunde_straße++</w:t></w:r></w:p></w:tc>
</w:tr>
<w:tr>
<w:tc><w:p><w:r><w:t>++plz++ ++stadt++</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++kunde_plz++ ++kunde_stadt++</w:t></w:r></w:p></w:tc>
</w:tr>
<w:tr>
<w:tc><w:p><w:r><w:t>Tel: ++telefon++</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t></w:t></w:r></w:p></w:tc>
</w:tr>
<w:tr>
<w:tc><w:p><w:r><w:t>Email: ++email++</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t></w:t></w:r></w:p></w:tc>
</w:tr>
</w:tbl>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Rechnungsnummer: ++rechnungsnummer++</w:t></w:r></w:p>
<w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Datum: ++datum++</w:t></w:r></w:p>
<w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Fällig: ++fälligkeitsdatum++</w:t></w:r></w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Rechnungsposten:</w:t></w:r></w:p>
<w:tbl>
<w:tblPr><w:tblW w:w="0" w:type="auto"/></w:tblPr>
<w:tr>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Pos.</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Artikel</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Menge</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Preis</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Gesamt</w:t></w:r></w:p></w:tc>
</w:tr>
<w:tr>
<w:tc><w:p><w:r><w:t>++positionen[0].nr++</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++positionen[0].artikel++</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++positionen[0].menge++</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++positionen[0].einzelpreis++</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>++positionen[0].gesamtpreis++</w:t></w:r></w:p></w:tc>
</w:tr>
</w:tbl>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Nettobetrag: ++nettobetrag++</w:t></w:r></w:p>
<w:p><w:r><w:rPr><w:b/></w:rPr><w:t>MwSt. (19%): ++mwst++</w:t></w:r></w:p>
<w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Gesamtbetrag: ++gesamtbetrag++</w:t></w:r></w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:t>Vielen Dank für Ihr Vertrauen!</w:t></w:r></w:p>
</w:body>
</w:document>`;
// Dateien zum ZIP hinzufügen
zip.addFile('[Content_Types].xml', Buffer.from(contentTypes, 'utf8'));
zip.addFile('_rels/.rels', Buffer.from(rels, 'utf8'));
zip.addFile('word/document.xml', Buffer.from(document, 'utf8'));
// DOCX speichern
const templatePath = path.join(__dirname, 'templates', 'rechnung-template.docx');
fs2.writeFileSync(templatePath, zip.toBuffer());
console.log('✅ Rechnungs-Template erstellt:', templatePath);
} catch (error) {
console.error('❌ Fehler beim Erstellen des Rechnungs-Templates:', error);
}
}
createInvoiceTemplateManual();