OfficeServer/create-brief-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

117 lines
3.8 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const AdmZip = require('adm-zip');
function createLetterTemplate() {
try {
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 - Brief 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="right"/></w:pPr>
<w:r><w:rPr><w:b/></w:rPr><w:t>++firma++</w:t></w:r>
</w:p>
<w:p>
<w:pPr><w:jc w:val="right"/></w:pPr>
<w:r><w:t>++straße++</w:t></w:r>
</w:p>
<w:p>
<w:pPr><w:jc w:val="right"/></w:pPr>
<w:r><w:t>++plz++ ++stadt++</w:t></w:r>
</w:p>
<w:p>
<w:pPr><w:jc w:val="right"/></w:pPr>
<w:r><w:t>Tel: ++telefon++</w:t></w:r>
</w:p>
<w:p>
<w:pPr><w:jc w:val="right"/></w:pPr>
<w:r><w:t>Email: ++email++</w:t></w:r>
</w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:t>++empfänger_name++</w:t></w:r></w:p>
<w:p><w:r><w:t>++empfänger_straße++</w:t></w:r></w:p>
<w:p><w:r><w:t>++empfänger_plz++ ++empfänger_stadt++</w:t></w:r></w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p>
<w:pPr><w:jc w:val="right"/></w:pPr>
<w:r><w:t>++ort++, ++datum++</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>++betreff++</w:t></w:r></w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:t>++anrede++,</w:t></w:r></w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:t>++einleitungstext++</w:t></w:r></w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:t>++haupttext++</w:t></w:r></w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:t>++schlusstext++</w:t></w:r></w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:t>++grußformel++</w:t></w:r></w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:t></w:t></w:r></w:p>
<w:p><w:r><w:t>++absender_name++</w:t></w:r></w:p>
<w:p><w:r><w:t>++position++</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:i/></w:rPr><w:t>Anlagen: ++anlagen++</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', 'brief-template.docx');
fs.writeFileSync(templatePath, zip.toBuffer());
console.log('✅ Brief-Template erstellt:', templatePath);
} catch (error) {
console.error('❌ Fehler beim Erstellen des Brief-Templates:', error);
}
}
createLetterTemplate();