Demo
Readme
Changelog
Releases RSS Feed
Compatibility : nodejs and browser

Summary (version 3.1.4) Buy error-location module

This module makes it possible to locate errors

This module is available as part of the docxtemplater ENTREPRISE plan.

README

Error location Module

This module makes it possible to locate errors in a word/document.

Installation:

You will need docxtemplater v3: npm install docxtemplater

Install this module with npm install --save "$url"

Usage

const ErrorLocationModule = require("docxtemplater-error-location-module");
function render() {
    const errorModule = new ErrorLocationModule();
    doc.attachModule(errorModule);
    // Now all your errors will be enhanced with locationWord
    try {
        doc.compile();
    } catch (e) {
        // In this catch block, the template is syntactically invalid, for example when writing following template :
        // `Hello {user, how are you ?`
        // Since the tag user is unclosed the template can not be compiled
        console.log("error during compilation");
        console.log(e);
        errorModule.addComments();
        const buffer = docx
            .getZip()
            .generate({ type: "nodebuffer", compression: "DEFLATE" });
        fs.writeFileSync("generated.docx", buffer);
        return;
    }
    try {
        doc.render();
    } catch (e) {
        // In this catch block, the template is syntactically valid, but the data cannot be applied to it.
        // This is usually related to an implementation issue, you could either show this error to your users or log it for a developpers inspection
        // If you use the angular_parser, please make sure that the expressions.compile method is directly inside the angularParser function, like this :
        // function angularParser(tag) {
        //     if (tag === '.') {
        //         return {
        //             get: function(s){ return s;}
        //         };
        //     }
        //     const expr = expressions.compile(tag.replace(/(’|“|”)/g, "'"));
        //     return {
        //         get: function(s) {
        //             return expr(s);
        //         }
        //     };
        // }
        // Also, if you use angular-expression filters, please make sure that your filters return the input if the type of the input is not the expected type :
        // expressions.filters.upper = function(input) {
        //     // This condition should be used to make sure that if your input is undefined, your output will be undefined as well and will not throw an error
        //     if(!input) return input;
        //     return input.toUpperCase();
        // }
        console.log("error during render");
        console.log(e);
        errorModule.addComments();

        const buffer = docx
            .getZip()
            .generate({ type: "nodebuffer", compression: "DEFLATE" });
        fs.writeFileSync("generated.docx", buffer);
        return;
    }
    const buffer = docx
        .getZip()
        .generate({ type: "nodebuffer", compression: "DEFLATE" });
    fs.writeFileSync("generated.docx", buffer);
}

In the browser, use following HTML file :

<html>
    <script src="node_modules/docxtemplater/build/docxtemplater.js"></script>
    <script src="node_modules/jszip/dist/jszip.js"></script>
    <script src="node_modules/jszip/vendor/FileSaver.js"></script>
    <script src="node_modules/jszip-utils/dist/jszip-utils.js"></script>
    <script src="build/error-location-module.js"></script>
    <script>
        JSZipUtils.getBinaryContent("examples/one-error.docx", function(
            error,
            content
        ) {
            if (error) {
                console.error(error);
                return;
            }
            var opts = {};

            var errorModule = new DocxtemplaterErrorLocationModule(opts);
            var zip = new JSZip(content);
            var doc = new docxtemplater()
                .loadZip(zip)
                .attachModule(errorModule);

            // Now all your errors will be enhanced with locationWord
            try {
                doc.compile();
            } catch (e) {
                console.log("error during compilation");
                console.log(e);
                errorModule.addComments();
                console.log("error", e);
                var out = doc.getZip().generate({
                    type: "blob",
                    mimeType:
                        "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                });
                saveAs(out, "generated.docx");
                return;
            }
            doc.setData({});
            try {
                doc.render();
            } catch (e) {
                console.log("error during render");
                console.log(e);
                errorModule.addComments();
                var out = doc.getZip().generate({
                    type: "blob",
                    mimeType:
                        "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                });
                saveAs(out, "generated.docx");
                return;
            }
            var out = doc.getZip().generate({
                type: "blob",
                mimeType:
                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
            });
            saveAs(out, "generated.docx");
        });
    </script>
</html>

After installing the module, you can use a working demo by running node sample.js.

Options

If you would like to change the author name and date of the comments, you can by using following code :

const errorModule = new ErrorLocationModule({
    author: "My author name",
    date: new Date()
});

date takes a Date parameter as an input, and author a string

try {
    doc.compile();
} catch (e) {
    errorModule.addComments();
    throw e;
}

Translation of messages

It is possible to translate the messages shown as comments in the document (if your end-users do not speak english), by providing a translation file.

An example with Dutch can be used with following code :

const errorModule = new ErrorLocationModule({
    translations: require("docxtemplater-error-location-module/translations/dutch.js")
});

Please contact me if you would like to have a translation for another language

CHANGELOG

3.1.4

  • Bugfix when comment is first text in the document, remove extra xml:space="preserve"

3.1.3

  • Update browser build to use XMLSerializer instead of xmldom

  • Use requiredAPIVersion

3.1.2

  • Fix browser build "TypeError: Argument 1 of XMLSerializer.serializeToString does not implement interface Node."

3.1.1

  • Move docxtemplater from devDependencies to dependencies

Explanation : On some versions of npm (notably 5.8.0), when having a package containing docxtemplater-error-location-module, the installation will generate a tree of node_modules that puts the module on a level where it has no access to docxtemplater. By explicitly asking it as a dependency, this issue is avoided.

3.1.0

Add support for language translation (with Dutch for now)

3.0.3

Make error-location module work with comments even when the HTML module is attached

3.0.2

Commenter does not change file if there are no errors

3.0.1

Make it possible to overwrite author + date of the comments

3.0.0

Initial release

Edgar Hipp

I'm the creator of docxtemplater. I work on making docxtemplater great since 2013.