docx icon indicating copy to clipboard operation
docx copied to clipboard

Table lose their formatting when viewed in Word Online

Open bladekey88 opened this issue 6 years ago • 3 comments

This library is phenomenal and really useful.

I generated a two clolumn table tables with borders in the document in broswer.

This all worked fine, and when viewing the downloaded document in Word 365 on the desktop, everything displays correctly.

However, when viewed through live.com (or the browser plugins), the table loses all its formatting, and reverts to both columns being 1 character wide, with no border. This is easily corrected by dragging the columns and re-applying the formatting, but for more complex tables this could be an issue (assuming it's not an issue with my code use).

Code Used:

`
<script>
    function exportToWord() {

        const subject = (results[0]['subjectid']['subjectname'])
        const topic = (results[0]['topicid']['topicname'])



        let doc = new docx.Document(
            {
        title: subject + ":" + topic + " Worksheet",
                creator: "Unknown",
                title: subject + ":" + topic + " Worksheet",
                description: "Exported Questions for " + topic,
                subject: subject,
                lastModifiedBy: "Unknown",
                revision: 1,
                })

    const mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"



    const docTitle =
        new docx.Paragraph({
        alignment: docx.AlignmentType.CENTER,
            children:
                [new docx.TextRun({
        text: subject + " Questions",
                    bold: true,
                    color: "black",
                    size: 48,
                    font: "Calibri",
                    smallCaps: true,
                })
                ],
        })

    const blankParagraph = new docx.Paragraph({
        text: "",
    });




    const topicParagraph =
        new docx.Paragraph({
        alignment: docx.AlignmentType.CENTER,
            children:
                [new docx.TextRun({
        text: "Topic - " + topic,

                    bold: true,
                    color: "black",
                    size: 28,
                    font: "Calibri",
                    smallCaps: true,
                })
                ],
        })

    const pageBreak = new docx.Paragraph({
        children: [new docx.PageBreak()],
    });


    const rowWorksheet = results.map(function (result) {
        return new docx.TableRow(
            {
        children: [
                    new docx.TableCell(
                        {
        children: [new docx.Paragraph(result.questiontext)],
                            width:
                            {
        size: 40,
                                type: docx.WidthType.PERCENTAGE,
                            }
                        }),
                    new docx.TableCell(
                        {
        children: [],
                            width:
                            {
        size: 60,
                                type: docx.WidthType.PERCENTAGE,
                            }
                        }),
                ],
            })
    })


    const tableblank = new docx.Table(
        {
        margins:
            {
        top: 250,
                bottom: 250,
                right: 200,
                left: 100,
            },
            cantSplit: true,
            rows: rowWorksheet,

        });


    doc.addSection(
        {
        headers: {
        default: new docx.Header({
        children: [
                new docx.Paragraph({
        children: [
                        new docx.TextRun("Downloaded From  "),
                    ],
                }),
            ],
        }),
    },
            footers: {
                default: new docx.Footer({

        children: [
                    new docx.Paragraph({
        children: [
                        new docx.TextRun({
        children: ["Page  ", docx.PageNumber.CURRENT],
                        }),
                        new docx.TextRun({
        children: [" of ", docx.PageNumber.TOTAL_PAGES],
                        }),
                    ],
                })


                     ],
                })

            },
            children: [

                docTitle,
                blankParagraph,
                topicParagraph,
                tableblank,


        ],
    });

    docx.Packer.toBlob(doc)
        .then(blob => {
        console.log(blob);
            saveAs(blob, subject + "_" + topic + "_worksheet.docx");
            console.log("Document created successfully");
        });

}

</script>
`

bladekey88 avatar May 20 '20 17:05 bladekey88

did you find something? i'm getting the same problem :c

kevinmao96 avatar Nov 20 '20 04:11 kevinmao96

No, so for the moment, I open it regular word, save it, then re-upload. It's not ideal, but it's a workaround.

bladekey88 avatar Nov 20 '20 21:11 bladekey88

I have found the issue, it is because you need to explicitly specify the widths

Most word processors aren't smart enough to figure out what width you want. Microsoft Word seems to be the exception and figures out the desired result nicely.

Check out this demo on how to specify widths (column widths and cell widths):

https://github.com/dolanmiu/docx/blob/master/demo/4-basic-table.ts

here are the results from different word processors using that demo:

Word 365:

Screenshot 2021-03-05 at 03 07 25

Libre Office:

Screenshot 2021-03-05 at 03 07 41

Google docs:

Screenshot 2021-03-05 at 03 07 50

In future, I will consider making docx "smarter" so it knows what width the user intends... but that seems to be a challenge in itself.

dolanmiu avatar Mar 05 '21 03:03 dolanmiu