LibreOffice不兼容docx-js自动表格宽度问题求助
解决docx生成表格在LibreOffice中无法显示的替代方案
针对使用docx 8.5.0生成表格时,省略宽度设置导致LibreOffice 24.8.0.3中表格宽度为0无法显示,且仅设置100%整体宽度不够理想的问题,提供以下几种可行方案:
为表格设置明确的列级宽度
不依赖整体表格宽度,给每个表格单元格单独分配宽度比例,让LibreOffice能直接识别各列尺寸,示例代码如下:const { Table, TableRow, TableCell, Paragraph, WidthType } = require("docx"); const table = new Table({ rows: [ new TableRow({ children: [ new TableCell({ width: { size: 30, type: WidthType.PERCENTAGE }, children: [new Paragraph("第一列内容")] }), new TableCell({ width: { size: 70, type: WidthType.PERCENTAGE }, children: [new Paragraph("第二列内容")] }) ] }) ] });用LibreOffice命令行做格式转换修复
利用LibreOffice的无头模式,先将生成的docx转成ODT格式,再转回docx,强制LibreOffice重新解析并生成兼容格式,之后再转PDF即可正常显示表格,命令如下:# 转成ODT libreoffice --headless --convert-to odt your-generated-document.docx # 转回DOCX libreoffice --headless --convert-to docx your-generated-document.odt手动修正document.xml中的表格属性
解压生成的docx文件,找到word/document.xml,在表格的<w:tblPr>节点下添加明确的表格宽度配置,同时为每个单元格的<w:tcPr>添加列宽设置:<!-- 表格级宽度设置 --> <w:tblPr> <w:tblW w:w="10000" w:type="pct"/> </w:tblPr> <!-- 单元格级宽度设置(示例) --> <w:tcPr> <w:tcW w:w="3000" w:type="pct"/> </w:tcPr>修改后重新打包成docx文件,LibreOffice即可正确识别表格宽度。
内容的提问来源于stack exchange,提问作者Bosko Sinobad
相关产品推荐
相关产品推荐

