Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrupted document after using Patcher with version 8.4.0 #2650

Open
adiRandom opened this issue Mar 19, 2024 · 4 comments
Open

Corrupted document after using Patcher with version 8.4.0 #2650

adiRandom opened this issue Mar 19, 2024 · 4 comments

Comments

@adiRandom
Copy link

I am trying to generate a document from a template using docx. After I run my code, if I open the resulting document using Google Docs it works just fine. But neither MS Office nor Apple Pages manage to open the file. And unfortunately, I need the files to open in Ms Office.

Here is the code I am using to generate the files, which I assume is correct since Google Docs can open the document:

export async function generateContract(contract: Contract, savePath: string) {
    console.log(__dirname)
    const template = await fs.readFile(path.join(__dirname, "template.docx"));
    const patches: Record<string, IPatch> = {};

    for (const key in contract) {
        const indexKey = key as keyof Contract;

        console.log(key)

        if (Array.isArray(contract[indexKey])) {
            patches[key] = getPatchForTable(contract[indexKey] as Array<Sale>);
        } else {
            patches[key] = getPatch(contract[indexKey] as PatchableField);
        }
    }

    const doc = await patchDocument(template, {
        patches: patches
    });

    await fs.writeFile(path.join(savePath, `${contract.contract_number.value}.docx`), doc);
}


type PatchableField = Field<string> | Field<number> | Field<Date>;

function getPatch(field: PatchableField): IPatch {
    console.log(field)

    let value: string
    if (typeof field.value === "string") {
        value = field.value;
    } else if (field.value instanceof Date) {
        value = formatDate(field.value as Date)
    } else {
        value = field.value.toString();
    }

    return {
        type: PatchType.PARAGRAPH,
        children: [new TextRun({
            text: value,
            bold: field.isBold,
            size: field.fontSize * 2,
            font: "Tahoma"
        })],
    }
}


// Get date as DD/MM/YYYY
function formatDate(date: Date): string {
    return `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`;
}

function getPatchForTable(sales: Array<Sale>): IPatch {
    console.log("table")
    const headerRow = new TableRow({
        height:{
            value: 600,
            rule: HeightRule.EXACT
        },
        children: [
            new TableCell({
                verticalAlign: "center",
                margins: {
                    top: 20,
                    bottom: 20,
                },
                children: [
                    new Paragraph({
                        children: [new TextRun({
                            text: "Nr. crt",
                            bold: true,
                            size: 20,
                            font: "Tahoma"
                        })]
                    }),
                ]
            }),

            new TableCell({
                verticalAlign: "center",
                children: [
                    new Paragraph({
                        children: [new TextRun({
                            text: "Casa de marcat (marca si modelul)",
                            bold: true,
                            size: 20,
                            font: "Tahoma"
                        })]
                    }),
                ]
            }),

            new TableCell({
                verticalAlign: "center",
                children: [
                    new Paragraph({
                        children: [new TextRun({
                            text: "Seria",
                            bold: true,
                            size: 20,
                            font: "Tahoma"
                        })]
                    }),
                ]
            }),

            new TableCell({
                verticalAlign: "center",
                children: [
                    new Paragraph({
                        children: [new TextRun({
                            text: "Adresa locului de amplasare",
                            bold: true,
                            size: 20,
                            font: "Tahoma"
                        })]
                    }),
                ]
            }),
        ]
    })

    return {
        type: PatchType.DOCUMENT,
        children: [new Table(
            {
                width: {
                    size: 100,
                    type: WidthType.AUTO,
                },
                columnWidths: [2500, 2500, 2500, 2500],
                indent: {
                    size: 450,
                    type: WidthType.DXA,
                },
                rows: [headerRow, ...sales.map(getPatchForSale)]
            }
        )]

    }
}

function getPatchForSale(sale: Sale): TableRow {
    return new TableRow({
        height:{
            value: 350,
            rule: HeightRule.EXACT
        },
        children: [
            new TableCell({
                verticalAlign: "center",
                shading: {
                    fill: "#ECECEC"
                },
                children: [
                    new Paragraph({
                        alignment: "right",
                        children: [new TextRun({
                            text: sale.id.toString(),
                            bold: false,
                            size: 20,
                            font: "Tahoma"
                        })]
                    }),
                ]
            }),

            new TableCell({
                verticalAlign: "center",
                shading: {
                    fill: "#ECECEC"
                },
                width: {size: 25, type: WidthType.PERCENTAGE},
                children: [
                    new Paragraph({
                        children: [new TextRun({
                            text: sale.item,
                            bold: false,
                            size: 20,
                            font: "Tahoma"
                        })]
                    }),
                ]
            }),

            new TableCell({
                verticalAlign: "center",
                shading: {
                    fill: "#ECECEC"
                },
                width: {size: 25, type: WidthType.PERCENTAGE},
                children: [
                    new Paragraph({
                        children: [new TextRun({
                            text: sale.sn,
                            bold: false,
                            size: 20,
                            font: "Tahoma"
                        })]
                    }),
                ]
            }),

            new TableCell({
                verticalAlign: "center",
                shading: {
                    fill: "#ECECEC"
                },
                width: {size: 25, type: WidthType.PERCENTAGE},
                children: [
                    new Paragraph({
                        children: [new TextRun({
                            text: sale.address,
                            bold: false,
                            size: 20,
                            font: "Tahoma"
                        })]
                    }),
                ]
            }),
        ]
    })
}

I also attached a document produced by my code.

15.docx

@PeKne
Copy link

PeKne commented Apr 28, 2024

I have a same issue. Any update?

@Titou325
Copy link

Same thing here :(

@othonam
Copy link

othonam commented Jun 14, 2024

This problem makes me waste a lot of time, because I was wondering the problem was my fault.
Given your opened issue @adiRandom, I tried open in Google docs, worked fine.
But I'm using patchDocument().

@Phillips126
Copy link

Phillips126 commented Jun 20, 2024

EDIT - I seemed to have resolved my issue by changing the PatchType to DOCUMENT, this is likely a misunderstanding on my part, but hopefully it may help someone else in the future:

image


I was experiencing the same issue.

Originally, I had been using this library to "patch" my master file, then convert the buffer data into a PDF using Libreoffice (command line), this worked fine for me, no issues. My client asked to just receive the docx file so they could make minor updates to the generated file - but for some reason, MS Word would throw the "Word experienced an error trying to open the file. Try these suggestions..." error every time. I dug into the internals a bit to try to figure out what was going on.

What I found, using an OOXML validation tool, was that my generated file was inserting the dynamic table as a child of a paragraph <w:p>, which is apparently not allowed according to the schema if I am reading the documentation correctly. It seems other applications are more forgiving, but MS Word just flat out refuses to open the document.

Here is an image of the document.xml snippet prior to using the patcher:
before

Here is an image of the document.xml snippet after using the patcher:
(note: I've collapsed the table code so you can see the open and close paragraph tags)
after

To open the patched document in MS Word again, I simply had to remove the parent <w:p> tags. Unfortunately, removing the parent <w:p> tags in the master template file (just leaving the {{PAYTABLE}}) does not work. The docx file is patched, but the tables do not get generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants