guigrpa / docx-templates

Template-based docx report creation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Corrupted statistics analyzer

Triplecorpse opened this issue · comments

Thank you for the module provided, it helps me a lot in my job. However, I would like to count number of pages in newly created document and it seems it fails to count. Here I provide the code and template. I run it with Node:

const templates = require("docx-templates");
const fsp = require("fs/promises");

(async function()  {
    const tpl = await fsp.readFile("tpl.docx")
    const uint = await templates.createReport({
        template: tpl,
        data: {
            text: "I love docx-templates".repeat(500)
        }
    })
    await fsp.writeFile("ext2.docx", uint);
    const file = await fsp.readFile("ext2.docx");
    const metadata = await templates.getMetadata(file);
    debugger;
})();

tpl.docx

My node version is:

C:\Users\eldar>node --version
v18.11.0

docx-templates version:

"docx-templates": "4.9.2",

Word statistics (4 pages):
image

docx-template statistics (1 page and everything else is incorrect):
image

Note: when I modify and save file in Word, the statistics counter begins to work as it should
Note 2: the same issue happens if I make Buffer.from(uint) without saving file

These statistics and other metadata can only be updated by Word after saving the file. Docx-templates only edits the xml of the docx file. You need a renderer like MS Word to actually count the number of pages that the docx file produces. I dont think it is possible to count the number of pages without rendering the entire file.