新手请教:在nodejs + express4.x下,使用哪种word模板导出word(最好有示例)

bohe2005 发布于 2018/03/07 18:37
阅读 1K+
收藏 1

环境:win10,Sublime Text,nodejs + express4.x(命令行运行)

需求:待生成word,内容较多,需要使用模板,以数据替换标签的方式进行处理

相关情况:

1、java中用过freemarker,github上有freemarker生成html的示例,但不知如何运行,相关js和bat都试过,无效果。

    github链接:https://github.com/ijse/freemarker.js

2、officegen生成word,目前查到和试验的是,使用nodejs写代码,插入样式、内容等

    链接:http://blog.csdn.net/liyanhui1001/article/details/45242359

 

问题:刚入门nodejs,想使用freemarker生成word,但不知道如何将数据传入其中,不知道freemarker对应模板标签的写法,陷在其中有一天了。

烦请知道的大大们,留个言,给个思路也好。谢谢。

加载中
0
TigaPile
TigaPile
https://github.com/open-xml-templating/docxtemplater 这个看看
0
bohe2005
bohe2005

引用来自“TigaPile”的评论

https://github.com/open-xml-templating/docxtemplater 这个看看

请问,您用这个处理过图片吗?我用 https://docxtemplater.com/modules/html/#changelog 处理图片,提示 module 不存在。如果屏蔽这段代码,word显示undefined。而相同的模板、数据放到 https://docxtemplater.com/demo/#image 可以生成word

router.get('/test3',function(req,res,next){
    //Load the docx file as a binary
    var content = fs.readFileSync(path.resolve(__dirname, 'template/inputXML3.docx'), 'binary');
    var zip = new JSZip(content);
    var doc = new Docxtemplater();
    doc.loadZip(zip);

    //set the templateVariables
    doc.setData({
        first_name: 'John',
        last_name: 'Doe',
        phone: '0652455478',
        description: 'New Website',
        //imgXml: '<p>Hello</p><img src="data:image/gif;base64,R0lGODlhDAAMAIQZAAAAAAEBAQICAgMDAwQEBCIiIiMjIyQkJCUlJSYmJigoKC0tLaSkpKampqenp6ioqKurq66urrCwsPb29vr6+vv7+/z8/P39/f7+/v///////////////////////////yH+EUNyZWF0ZWQgd2l0aCBHSU1QACwAAAAADAAMAAAFSiCWjRmViVVmZRWWjiurXrBI2uMkFcdjrRfaJQIQCAKQTDBmKAYACAwuswAABoBEDMUgYAkOpWiFeRgUDZIKNnqpLaJLXM2KjaYhADs=" alt="">',
        imgXml: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4QIJBywfp3IOswAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAkUlEQVQY052PMQqDQBREZ1f/d1kUm3SxkeAF/FdIjpOcw2vpKcRWCwsRPMFPsaIQSIoMr5pXDGNUFd9j8TOn7kRW71fvO5HTq6qqtnWtzh20IqE3YXtL0zyKwAROQLQ5l/c9gHjfKK6wMZjADE6s49Dver4/smEAc2CuqgwAYI5jU9NcxhHEy60sni986H9+vwG1yDHfK1jitgAAAABJRU5ErkJggg==',
        rawXml: '<w:p><w:pPr><w:rPr><w:color w:val="FF0000"/></w:rPr></w:pPr><w:r><w:rPr><w:color w:val="FF0000"/></w:rPr><w:t>My custom</w:t></w:r><w:r><w:rPr><w:color w:val="00FF00"/></w:rPr><w:t>XML</w:t></w:r></w:p>'
    });

    //start: 屏蔽代码,生成word图片标签显示为undefined
    const HTMLModule = require("docxtemplater-html-module");
    const ImageModule = require("docxtemplater-image-module");
    const htmlModule = new HTMLModule({
        img: {
            Module: ImageModule,
            // By default getSize returns the width and height attributes if both are present, or 200x200px as a default value.
            getSize: function(data) {
                // The html element, for example, if the data is :
                // '<img height="20" src="...">'
                // you will have data.element.attribs.width = '20'
                console.log(data.element);
                // The arraybuffer of your image
                // (you could use https://github.com/image-size/image-size to calculate the size)
                console.log(data.src);
                // You return an array in pixel (here we have width 50px and height 100px)
                return [50, 100];
            }
        }
    });
    doc.attachModule(htmlModule);
    //end: 屏蔽代码,生成word图片标签显示为undefined

    try {
    // render the document (replace all occurences of {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}));
        // The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
        throw error;
    }

    var buf = doc.getZip().generate({type: 'nodebuffer'});
    // buf is a nodejs buffer, you can either write it to a file or do anything else with it.
    fs.writeFileSync(path.resolve(__dirname, '../../outfiles/outputXMLtest3.docx'), buf);
    res.render('docxtemplater/docx1', { msg: '创建doc结束(xml的test示例3)(测试xm基础上增加img替换)'});
});

模板截图

使用上述 module 后,错误截图

vscode 里面install时,提示找不到 “docxtemplater-html-module”

返回顶部
顶部