Edit document doc (x) with node.js / javascript

4

I am trying to get the data collected from the user sent to a doc (x) text document stored on a server, so that the data completes a template. So far I have only found an Office API for JavaScript, but I'm worried about compatibility issues or permissions.

Is there a way to enter the parameters collected in a document without using this API?

The idea is not simply to store the parameters in a doc, but to store them in specific spaces of an existing document.

    
asked by Pablo León 15.07.2017 в 13:40
source

3 answers

1

I give you an example of how I do it now and the author's website. This is a service that I call passing the json with the keys I want to replace and the path to the .docx file that I have as a template.

Dependencies

{
    "docxtemplater": "^3.6.3",
    "jszip": "^2.6.1"
}

link

Greetings, I hope it serves you

var JSZip = require('jszip');
var Docxtemplater = require('docxtemplater');

var fs = require('fs');
var path = require('path');

module.exports = getDoc = function (data, file) {
    // Cargo el docx como un  binary
    var content = fs.readFileSync(path.resolve( file), 'binary');

    var zip = new JSZip(content);

    var doc = new Docxtemplater();
    doc.loadZip(zip);

    // setea los valores de data ej: { first_name: 'John' , last_name: 'Doe'}
    doc.setData(data);

    try {
        // renderiza el documento (remplaza las ocurrencias como {first_name} by John, {last_name} by Doe, ...)
        doc.render();
    }
    catch (error) {
        var e = {
            message: error.message,
            name: error.name,
            stack: error.stack,
            properties: error.properties,
        }
        console.log(JSON.stringify({ error: e }));
        throw error;
    }

    var buffer = doc.getZip()
        .generate({ type: 'nodebuffer' });

    fs.writeFileSync(path.resolve('tmpdocs', data.doc + file), buffer);

    return path.resolve(__dirname.replace('tmpdocs', data.doc + file);
}
    
answered by 10.06.2018 / 22:19
source
1
  

Is there a way to enter the parameters collected in a document without using that API?

Short answer

Assuming that by doc (x) you refer to a .doc or .docx file, it is possible but it generally does not make sense to "reinvent the wheel".

Explanation

The .doc format is a binary format, the .docx format is a "composite" format, since it is a compressed file that includes several files of which the main ones are XML files and the rest are resources, for example, images.

On the other hand JavaScript libraries what it does is save you work, but one should take time to know its focus and scope.

If you insist on using pure JavaScript and assuming you decide to use .docx, the following is what "occurs to me" without having reviewed the documentation

  • Obtain the .docx file and the data provided by the user
  • Unzip the .docx file and select the main file.
  • Repeat for each element:
    • If the structure is known, then use select the element and insert the content provided by the user.
    • If the structure is not known, there must be a map or dictionary that establishes the relationship of the data provided by the user with its location in the document.
  • Compress the files in a new .docx
  • Save the new .docx file

It is worth mentioning that Microsoft Word has the ability to create forms and insert fields to display both built-in and custom properties, protect the document and protect fields, etc. however, not all editors that .doc and .docx files can handle files in which these features are used, besides that this could "complicate" the code because those elements should be considered.

    
answered by 22.07.2017 в 18:16
1

There are several javascript libraries either just for the browser, only for node or usable in both systems that allow you to manipulate, interact or use docx files as templates.

For example (comments to September 2018):

  • DOCX.js . Which is very outdated and probably does not work properly.
  • html-docx-js . That allows you to convert an html document to docx.
  • officegen . A library for node (with plans to work in the browser ) that allows generating files in different formats MS-Office.
  • docx-template . It allows to use docx files as template and fill it with variables js.
  • docx-templates . It allows to use docx files as template and fill it with variables js.
  • generate-docx . Only nodejs, it does not work in the browser. It allows to use docx files as template and fill it with js variables. It's actually a docxtemplater wrapper that has many of its features missing like include images . It contributes very little on docxtemplater.
  • docxtemplater . It allows to use docx files as template and fill it with variables js.

From these libraries I have only tried docxtemplater and we are using it in production in several projects. It is Free Software with some payment plugins and allows for example to include images.

    
answered by 22.07.2017 в 17:06