G
G
Goobert Crypto2020-01-28 23:05:54
JavaScript
Goobert Crypto, 2020-01-28 23:05:54

How to fill a word template using js?

There is a ready-made Word template, how can I fill it out on the site using a form into which data is entered created in js? After filling out the form, the completed template is downloaded from the server.

What library is required to perform these tasks? If she's needed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Shohruh Shaimardonov, 2020-01-28
@joeberetta

Read the off.doc of the small software . And so, here is a lib on npm

M
MikhedValery, 2021-05-06
@MikhedValery

In the docx template, mark fields for insertion {field1} {field2}, etc. close
In your
docx program, open it as a regular archive, extract the word/document.xml file from it.
Edit with a replacement as plain text. (First, using regular expressions, extra '<.*?>')
For example, I tried directly in JSzip

function isprXML(xmlfile) {
            // //почистить шаблон до правильного вида переменных {field1}
            var re = /({.*?})/sg;
            var re2 = /(<.*?>)/g;;
            let result = xmlfile.match(re) || [];
            let newres = [];
            result.forEach(element => {
                var newel = element.replace(re2, "");
                xmlfile = xmlfile.replace(element, newel);
            });
            return xmlfile;
        }

Then just replace the {field1} fields with your data
xmlfile = xmlfile.replace("field1", data);
word/document.xml - Put back in the archive
Here is an example in JS - works directly in the browser

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question