Coder Social home page Coder Social logo

Comments (1)

edi9999 avatar edi9999 commented on June 6, 2024

Hello,

I remember someone doing something very similar a few years back. (Maybe it was you ?). I could'n t find whether it was on a github issue or per email.

I think the way he did it at the time was, before passing the data to docxtemplater, he would open the word/document.xml, and add unique ids to each <w:t> tag.

Then you could generate your docx from that same zip file.

Something like this (untested code, but likely should work) :

const zip = new JSZip(buf);
const text = zip.file("word/document.xml").asText();
const { DOMParser, XMLSerializer } = require('@xmldom/xmldom')
const document = new DOMParser().parseFromString(text, "text/xml");
const textTags = document.getElementsByTagName("w:t");
let trackedId = 1;
for (let i = 0, len = textTags.length; i < len; i++) {
	const textTag = textTags[i];
	textTag.setAttribute("_trackedId", trackedId++)
}
const newText = new XMLSerializer().serializeToString(document);
zip.file("word/document.xml", newText);
const doc = new Docxtemplater(zip, { paragraphLoop: true, linebreaks: true });
doc.render({
     first_name: "John",
});

I'm not exactly sure how you then convert your XML to HTML and keep the tags, if you have more insights, please post them here so that future users can also benefit from this !

from docxtemplater.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.