Refactor DOCX file generation to simplify path handling and enhance document styles

This commit is contained in:
Leon Bösche
2026-01-14 11:48:07 +01:00
parent 5cb86d4fde
commit 3757368a00

View File

@@ -258,19 +258,9 @@ class FileService {
// Generate minimal valid DOCX file (Office Open XML format)
final bytes = _generateEmptyDocx();
// Construct proper path
String fullPath;
if (parentPath == '/') {
fullPath = '/$docxName';
} else {
final cleanParent = parentPath.endsWith('/')
? parentPath.substring(0, parentPath.length - 1)
: parentPath;
fullPath = '$cleanParent/$docxName';
}
// Send parent directory as 'path' parameter
final formData = FormData.fromMap({
'path': fullPath,
'path': parentPath,
'file': MultipartFile.fromBytes(bytes, filename: docxName),
});
@@ -296,6 +286,7 @@ class FileService {
<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"/>
<Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>
</Types>''';
archive.addFile(
ArchiveFile(
@@ -321,13 +312,17 @@ class FileService {
// word/document.xml - the actual document content (empty)
const documentXml =
'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="w14">
<w:body>
<w:p>
<w:r>
<w:t></w:t>
</w:r>
<w:pPr>
<w:pStyle w:val="Normal"/>
</w:pPr>
</w:p>
<w:sectPr>
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
</w:sectPr>
</w:body>
</w:document>''';
archive.addFile(
@@ -338,6 +333,46 @@ class FileService {
),
);
// word/styles.xml - document styles
const stylesXml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="w14">
<w:docDefaults>
<w:rPrDefault>
<w:rPr>
<w:rFonts w:ascii="Calibri" w:hAnsi="Calibri" w:cs="Calibri" w:eastAsia="Calibri"/>
<w:sz w:val="22"/>
<w:szCs w:val="22"/>
<w:lang w:val="en-US" w:eastAsia="en-US" w:bidi="ar-SA"/>
</w:rPr>
</w:rPrDefault>
<w:pPrDefault>
<w:pPr>
<w:spacing w:after="160" w:before="0" w:line="259" w:lineRule="auto"/>
</w:pPr>
</w:pPrDefault>
</w:docDefaults>
<w:style w:type="paragraph" w:default="1" w:styleId="Normal">
<w:name w:val="Normal"/>
<w:qFormat/>
<w:pPr>
<w:spacing w:after="160" w:before="0" w:line="259" w:lineRule="auto"/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Calibri" w:hAnsi="Calibri" w:cs="Calibri" w:eastAsia="Calibri"/>
<w:sz w:val="22"/>
<w:szCs w:val="22"/>
<w:lang w:val="en-US" w:eastAsia="en-US" w:bidi="ar-SA"/>
</w:rPr>
</w:style>
</w:styles>''';
archive.addFile(
ArchiveFile(
'word/styles.xml',
stylesXml.length,
Uint8List.fromList(stylesXml.codeUnits),
),
);
// word/_rels/document.xml.rels - document relationships (empty but required)
const docRels = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">