opensagres / xdocreport

XDocReport means XML Document reporting. It's Java API to merge XML document created with MS Office (docx) or OpenOffice (odt), LibreOffice (odt) with a Java model to generate report and convert it if you need to another format (PDF, XHTML...).
https://github.com/opensagres/xdocreport
1.19k stars 369 forks source link

Convert to PDF error with grid #621

Open valimaties opened 1 year ago

valimaties commented 1 year ago

Hi. Trying to convert a DOCX document build with Apache POI 5.2.3 will give me this error:

Cannot invoke "org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid.getGridColList()" because "grid" is null
    at fr.opensagres.poi.xwpf.converter.core@2.0.4/fr.opensagres.poi.xwpf.converter.core.utils.XWPFTableUtil.getGridColList(XWPFTableUtil.java:180).

My document loads from a template which contains an image inside. I add paragraphs and tables inside, it's ok when I save it as docx and open it as docx. But using the generated XWPFDocument to convert it to PDF using this library gives me this error.

Any clue?

EDIT:

This is the start of the table inside "document.xml" file from the docx file:

(...)
                  <w:tbl>
            <w:tblPr>
                <w:tblW w:w="5000" w:type="pct"/>
                <w:jc w:val="center"/>
                <w:tblBorders>
                    <w:top w:val="single"/>
                    <w:left w:val="single"/>
                    <w:bottom w:val="single"/>
                    <w:right w:val="single"/>
                    <w:insideH w:val="single"/>
                    <w:insideV w:val="single"/>
                </w:tblBorders>
            </w:tblPr>
            <w:tblPr>
                <w:tblStyle w:val="Table Grid"/>
            </w:tblPr>
            <w:tr>
                <w:trPr>
                    <w:tblHeader w:val="on"/>
                </w:trPr>
                <w:tc>
                    <w:tcPr>
                        <w:shd w:color="auto" w:val="clear" w:fill="C0C0C0"/>
                        <w:vAlign w:val="center"/>
                    </w:tcPr>
(...)
valimaties commented 1 year ago

Ok... I found the issue here: I created the columns dinamically, by adding cells to the row, not creating columns specifically! Now, adding columns to the grid, the code from the document.xml file looks like this:

(...)
                   <w:tbl>
            <w:tblPr>
                <w:tblW w:w="5000" w:type="pct"/>
                <w:jc w:val="center"/>
                <w:tblBorders>
                    <w:top w:val="single"/>
                    <w:left w:val="single"/>
                    <w:bottom w:val="single"/>
                    <w:right w:val="single"/>
                    <w:insideH w:val="single"/>
                    <w:insideV w:val="single"/>
                </w:tblBorders>
            </w:tblPr>
            <w:tblPr>
                <w:tblStyle w:val="Table Grid"/>
            </w:tblPr>
            <w:tblGrid>
                <w:gridCol w:w="500"/>
                <w:gridCol w:w="1000"/>
                <w:gridCol w:w="1000"/>
                <w:gridCol w:w="2000"/>
                <w:gridCol w:w="500"/>
            </w:tblGrid>
(...)

However, I consider this a bug! A table does not need columns, but only rows cells... Columns could be read from how many cells are in the row. This is a simple table, where somehow the columns could be defined. But there could be tables where "a column" could contain multiple cells, or the cells in the header could be merged, splitted, and so on... so, we must have rows with cells, which have some width and height...

angelozerr commented 1 year ago

However, I consider this a bug!

Any contribution are welcome!