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 · 2 comments
Open

Corrupted document after using Patcher with version 8.4.0 #2650

adiRandom opened this issue Mar 19, 2024 · 2 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 :(

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

3 participants