前端node生成word文件调研记录(art-template,officegen,docxtemplater)

node 生成word ,调研的技术有3种,下面分别说明优缺点:

应用场景:根据模板生成word文件,需要有页眉页脚,最好可以像模块一样,导入多个文件,生成一个文件

1.art-template + html-docx-js

官网:介绍 - art-template

优点:语法简单,使用方便,可以给每个art文件导入样式,支持多文件拼接,拼接内容之间无间隙,预览很方便,可以直接使用模板里的HTML

缺点:因为每个art文件的内容不固定,所以没办法设置页眉页脚

使用方式如下:

npm install art-template

art模板文件的语法可以参照官网,官网有详细说明

art1.art文件内容

<h1>{{title}}</h1>

<p><strong>docxtemplater</strong> is a library to generate docx/pptx
documents from a docx/pptx template. It can replace {placeholders}
with data and also supports loops and conditions. The templates can be
edited by non-programmers, eg for example your clients.</p>

<p>Installation: <code>npm install docxtemplater</code></p>


<code> npm install && npm run compile </code>

art2.art文件内容:

<style>
{{include './custom2.css'}}
</style>
<p class='title2'>This is just an awesome page.</p>

custom2.css

.title2{
  color:green
}

index.js文件内容

const template = require("art-template");
const path = require("path");
const htmlDocx = require("html-docx-js");
const fs = require("fs");
const stream = require("stream");
// 用于存储文件数量,可以让每次生成文件的名称不一样
const LocalStorage = require("node-localstorage").LocalStorage;
const localStorage = new LocalStorage("./scratch");
let count = localStorage.getItem("count") || 0;


// 获取模板
function getTemplate(artFileName) {
  const views = path.join(__dirname, "../views", artFileName + ".art"); // 模板路径
  const htmlStr = template(views, {
    title: "这是第二个内容",
  });
  return htmlStr;
}

// 拼接模板 生成word
async function jointTemplate(templateArr) {
  let htmlStr = "";
  templateArr.forEach((item) => {
    htmlStr += getTemplate(item);
  });
  count = count - 0 + 1;
  localStorage.setItem("count", count);
  const fileName = `测试Demo${count}`;
  const htmlBlob = htmlDocx.asBlob(htmlStr, { orientation: "landscape" }); //buffer
  const docxPath = path.join(__dirname, "../views", fileName + ".docx");

  // 创建一个bufferstream
  const bufferStream = new stream.PassThrough();
  //将Buffer写入
  bufferStream.end(htmlBlob);
   //创建文件流写入文件
  var out = await fs.createWriteStream(docxPath);
  //创建文件流写入文件
  bufferStream.pipe(out);
  //读取文件流
  // const fileContent = fs.readFileSync(docxPath, "binary");
  return new Promise((resolve, reject) => {
    resolve("");
  });
}

 const params = ['art1','art2']
 jointTemplate(params).then((result) => {
    res.json(result);
 });

结果如下所示:

2.officegen

这个很类似给Html添加子元素一样。创建一个元素,给元素添加内容,设置样式

官网:没有官网

gitHub:https://github.com/Ziv-Barber/officegen#readme 

优点:语法简单,使用方便,可以设置页眉页脚

缺点:页脚不能动态的设置页码,不能多文件拼接,无法适应复杂需求,预览需要word转html

使用方式如下:

npm install officegen

const path = require("path");
const fs = require("fs");
const officegen = require("officegen");
const LocalStorage = require("node-localstorage").LocalStorage;
const localStorage = new LocalStorage("./scratch");
let count = localStorage.getItem("count") || 0;


async function jointTemplate() {
  count = count - 0 + 1;
  localStorage.setItem("count", count);
  const fileName = `测试Demo${count}`;
  const docxPath = path.join(__dirname, "../views", fileName + ".docx");

  var docx = officegen("docx");
  var out = await fs.createWriteStream(docxPath); // 文件写入
  //修改文字大小字体等
  let pObj = docx.createP();

  pObj.addText("Simple");
  pObj.addText(" with color", { color: "000088" });
  pObj.addText(" and back color.", { color: "00ffff", back: "000088" });

  pObj = docx.createP();

  pObj.addText("Since ");
  pObj.addText("officegen 0.2.12", {
    back: "00ffff",
    shdType: "pct12",
    shdColor: "ff0000",
  }); // Use pattern in the background.
  pObj.addText(" you can do ");
  pObj.addText("more cool ", { highlight: true }); // Highlight!
  pObj.addText("stuff!", { highlight: "darkGreen" }); // Different highlight color.

  pObj = docx.createP();

  pObj.addText("Even add ");
  pObj.addText("external link", { link: "https://github.com" });
  pObj.addText("!");
  // pObj.addText(fileContent, { font_face: "仿宋", font_size: 18 });
  // 添加页眉
  var header = docx.getHeader().createP();
  header.addText("这是页眉你来添加内容");
  // 添加页脚
  var footer = docx.getFooter().createP();
  footer.addText("这是页脚你来添加内容", {
    align: "center",
  });
  docx.generate(out);

  return new Promise((resolve, reject) => {
    resolve("");
  });
}


  jointTemplate().then((result) => {
    res.json(result);
  });

生成效果如下:

3.docxtemplater

以word文件为模板,读取之后填充变量的内容,再写入文件

官网:Documentation | docxtemplater

优点:语法还好,使用方便,可以设置页眉页脚,可以动态设置变量,如果购买了的话,就可以支持html,这样预览很方便,可以直接使用插入的html,或者art-template和docxtemplate结合使用

缺点:不支持文件拼接,但可以使用docx-merger 来拼接文件,这个拼接文件有一点缺陷,样式可能会丢失(其他文章说的,暂无发现,可能我的例子比较简单),拼接的内容会重起一页进行拼接,不会直接接在上一个文件内容的下面,docx-merge会在补充里说到

这个插件支持xml,html,以及table,image等等,具体可以研究官方文档。
xml是免费的,docx模板的语法是{@rawXml},rawXml是变量。在index.js中赋值就可以。
但是html是收费的,docx模板的语法是{~~html},html是变量,在index.js中赋值就可以

使用方式如下:

npm install docxtemplater

docx模板:不知道怎么上传,就截图吧

index.js

const path = require("path");
const fs = require("fs");
const expressionParser = require("docxtemplater/expressions.js");
var PizZip = require("pizzip");
const Docxtemplater = require("docxtemplater");
const libre = require("libreoffice-convert");
libre.convertAsync = require("util").promisify(libre.convert);
const LocalStorage = require("node-localstorage").LocalStorage;
const localStorage = new LocalStorage("./scratch");
let count = localStorage.getItem("count") || 0;

// 拼接模板 生成word
async function jointTemplate() {
  // let htmlStr = getTemplate(templateArr[0]);
  count = count - 0 + 1;
  localStorage.setItem("count", count);
  const fileName = `测试Demo${count}`;


  const docxPath = path.join(__dirname, "../views", fileName + ".docx");
  const docxPath2 = path.resolve(__dirname, "../views/template3.docx");
  // const pdfPath = path.join(__dirname, "../views", fileName + ".pdf");

  const content = fs.readFileSync(docxPath2, "binary");
  var zip = new PizZip(content);
  var doc = new Docxtemplater(zip, { parser: expressionParser });

  doc.setData({
    unit: "xxx单位",
    list: [
      {
        option: 1,
        monitorName: "11111",
        imageType: "图像",
        monitorProject: "视频",
        time: "2023-09-09 09:09:09",
      },
      {
        option: 2,
        monitorName: "222222",
        imageType: "图像2",
        monitorProject: "视频2",
        time: "2023-09-09 09:09:09",
      },
      
    ],
    isImportant: false,
    testIf: true,
    isTrue: 4,
   
  });
  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" });

  // docx2pdf(buf, pdfPath);

  // buf is a nodejs buffer, you can either write it to a file or do anything else with it.
  fs.writeFileSync(docxPath, buf);
  return new Promise((resolve, reject) => {
    resolve("");
  });
}
// word 生成pdf
async function docx2pdf(docxBuf, outputPath) {
  // Convert it to pdf format with undefined filter (see Libreoffice docs about filter)
  libre.convert(docxBuf, "pdf", undefined, (err, outputBuf) => {
    fs.promises.writeFile(outputPath, outputBuf);
  });

}

可以看到代码里有用到libreoffice-convert,这个是word转pdf的,补充里会说到

代码里还用到了这个,docxtemplater/expressions.js,条件语法需要用到这个来解析

生成的效果如下:

根据需求最后选择了docxtemplater,这个可以最大程度的满足需求,且模板使用方便

补充:

因为需求还要生成pdf,考虑到word可以转pdf,就用了libreoffice-convert这个插件

1.libreoffice-convert

官网:没找到官网

gitHub:GitHub - elwerene/libreoffice-convert

使用这个插件之前需要安装 软件:libreoffice,地址:Home | LibreOffice - Free Office Suite - Based on OpenOffice - Compatible with Microsoft

如果是mac:直接下载安装

window:下载msi文件

linux:用包管理器安装

翻了下源码,他的原理大概就是读取文件,用libreoffice软件转成pdf,再写入,然后我们再读取,再写入到设置的目录中,所以需要安装libreoffice软件

报错解决:

如果遇到这个报错:Could not find soffice binary

​​​​​​​是因为没有安装软件libreoffice

安装一下可能会遇到下一个报错:

[Error: ENOENT: no such file or directory, open '/var/folders/jb/lj7y6n2107q7zk47582wds6h0000gn/T/libreofficeConvert_-26532-dgbge6muCOJX/source.pdf'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/var/folders/jb/lj7y6n2107q7zk47582wds6h0000gn/T/libreofficeConvert_-26532-dgbge6muCOJX/source.pdf'
}

这个报错是因为文件没有写入成功,找不到文件。可以检查下我们的代码,官方的写法是

// Convert it to pdf format with undefined filter (see Libreoffice docs about filter)
    let pdfBuf = await libre.convertAsync(docxBuf, ext, undefined);
    
// Here in done you have pdf file which you can save or transfer in another stream
    await fs.writeFile(outputPath, pdfBuf);

可以改成下面这种写法试一下:

const pdfPath = path.join(__dirname, "../views",  "123.pdf");

const docxPath5 = path.resolve(__dirname, "../views/template3.docx");

const docxBuf = fs.readFileSync(docxPath5);

libre.convert(docxBuf, "pdf", undefined, (err, outputBuf) => {

fs.promises.writeFile(pdfPath, outputBuf);

});

用法如下所示,接着上面docxtemplater中index.js 的代码:

2.docx-merge

可以参考这篇文章

https://blog.csdn.net/CHANCE_wqp/article/details/131275952

使用如下

const path = require("path");
const fs = require("fs");
var PizZip = require("pizzip");
const Docxtemplater = require("docxtemplater");
const DocxMerger = require("docx-merger");
const LocalStorage = require("node-localstorage").LocalStorage;
const localStorage = new LocalStorage("./scratch");
let count = localStorage.getItem("count") || 0;

// 拼接模板
// templateArr - 文件名称列表
async function jointTemplate() {
  count = count - 0 + 1;
  localStorage.setItem("count", count);
  const fileName = `测试Demo${count}`;

  const docxPath = path.join(__dirname, "../views", fileName + ".docx");
  const docxPath2 = path.resolve(__dirname, "../views/template3.docx"); // 模板文件
  const docxPath4 = path.resolve(__dirname, "../views/testDoc.docx"); // 需要合并的文件(获取需要合并的模板文件)

  const fileContent = fs.readFileSync(docxPath4, "binary");
  const content = fs.readFileSync(docxPath2, "binary");
  var zip = new PizZip(content);
  var doc = new Docxtemplater();
  doc.loadZip(zip);
  doc.setData({
    unit: "xxx单位",
    list: [
      {
        option: 1,
        monitorName: "1监测点",
        imageType: "图像",
        monitorProject: "视频",
        time: "2023-09-09 09:09:09",
      },
      {
        option: 2,
        monitorName: "2监测点",
        imageType: "图像2",
        monitorProject: "视频2",
        time: "2023-09-09 09:09:09",
      },
      
    ],
    isImportant: false,
    testIf: true,
  });
  const zip2 = new PizZip(fileContent);
  var doc2 = new Docxtemplater();
  doc2.loadZip(zip2);

  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" });
  const buf2 = doc2.getZip().generate({ type: "nodebuffer" });
  // content 不是buffer
  var docx = new DocxMerger({}, [buf,buf2 ]);
  docx.save("nodebuffer", function (data) {
    fs.writeFile(docxPath, data, function (err) {
      console.log(err);
    });
  });
  return new Promise((resolve, reject) => {
    resolve('');
  });
}

 jointTemplate().then((result) => {
    res.json(result);
  });

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 首先,你需要安装 `art-template` 和 `mysql` 模块,使用以下命令: ``` npm install art-template mysql ``` 接下来,你需要编写一个简单的登录页面。在这个页面中,你需要一个表单来输入用户名和密码,并且在提交表单时发送 POST 请求到后端。例如: ```html <form action="/login" method="POST"> <div> <label for="username">用户名</label> <input type="text" id="username" name="username"> </div> <div> <label for="password">密码</label> <input type="password" id="password" name="password"> </div> <button type="submit">登录</button> </form> ``` 在后端,你需要使用 `express` 模块来处理路由和请求,使用 `mysql` 模块来连接和操作数据库,使用 `art-template` 模板引擎来渲染页面。以下是一个简单的后端代码示例: ```javascript const express = require('express'); const mysql = require('mysql'); const template = require('art-template'); const app = express(); // 创建 MySQL 连接池 const pool = mysql.createPool({ host: 'localhost', user: 'root', password: 'password', database: 'database_name' }); // 处理登录请求 app.post('/login', (req, res) => { const { username, password } = req.body; // 查询用户数据 pool.query('SELECT * FROM users WHERE username = ?', [username], (error, results) => { if (error) throw error; // 校验密码 if (results.length > 0 && results[0].password === password) { // 登录成功,渲染成功页面 const html = template(__dirname + '/success.html', { username }); res.send(html); } else { // 登录失败,渲染失败页面 const html = template(__dirname + '/failure.html'); res.send(html); } }); }); app.listen(3000, () => { console.log('Server started on port 3000'); }); ``` 在这个示例中,我们假设数据库中有一个名为 `users` 的表,包含 `id`、`username` 和 `password` 三个字段。当用户提交登录表单时,我们首先查询数据库中是否有匹配的用户名和密码,如果有则渲染登录成功的页面,否则渲染登录失败的页面。 在 `success.html` 页面中,你可以根据需要添加一些用户信息或操作,例如: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>登录成功</title> </head> <body> <h1>欢迎,{{ username }}!</h1> <p>你已经成功登录。</p> <a href="/">返回首页</a> </body> </html> ``` 在 `failure.html` 页面中,你可以添加一些错误提示或重新登录的链接,例如: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>登录失败</title> </head> <body> <h1>登录失败</h1> <p>用户名或密码错误,请重新登录。</p> <a href="/">返回首页</a> </body> </html> ``` 以上代码仅供参考,你可以根据实际情况进行调整和扩展。 ### 回答2: 要使用Node.js和MySQL来实现登录成功后页面的变化,可以按照以下步骤进行操作: 1. 首先,需要安装`node.js`、`mysql`和`art-template`模块。可以使用以下命令进行安装: ``` npm install node.js mysql art-template ``` 2. 在Node.js中,创建一个`server.js`文件,并在文件中引入必要的模块: ```javascript const http = require('http'); const mysql = require('mysql'); const template = require('art-template'); ``` 3. 连接到MySQL数据库: ```javascript const connection = mysql.createConnection({ host: 'localhost', //数据库服务器地址 user: 'root', //数据库用户名 password: 'password', //数据库密码 database: 'database_name' //数据库名称 }); connection.connect((err) => { if (err) { console.error('数据库连接失败!', err); } else { console.log('数据库连接成功!'); } }); ``` 4. 创建一个HTTP服务器: ```javascript const server = http.createServer((req, res) => { // 处理登录请求 if (req.url === '/login' && req.method === 'POST') { const body = []; req.on('data', (chunk) => { body.push(chunk); }); req.on('end', () => { const data = JSON.parse(body.toString()); // 查询数据库,验证用户名和密码是否正确 const sql = `SELECT * FROM users WHERE username = ? AND password = ?`; const values = [data.username, data.password]; connection.query(sql, values, (err, results, fields) => { if (err) { console.error('登录失败!', err); res.statusCode = 500; res.end(); } else if (results.length > 0) { // 使用art-template生成登录成功后的页面 const html = template.render('success.html', { username: data.username }); res.statusCode = 200; res.setHeader('Content-Type', 'text/html'); res.end(html); } else { // 登录失败 res.statusCode = 401; res.end(); } }); }); } else { // 返回登录页面 const html = template.render('login.html'); res.statusCode = 200; res.setHeader('Content-Type', 'text/html'); res.end(html); } }); server.listen(3000, () => { console.log('服务器已启动,监听端口3000'); }); ``` 5. 在项目根目录下创建`login.html`和`success.html`文件,并定义对应的登录和成功页面内容。 以上是一个简单的使用Node.js、MySQL和art-template模板引擎实现登录成功后页面的变化的示例。根据具体项目需求,你可能需要进一步完善和调整代码。 ### 回答3: 要使用Node.js和MySQL来实现登录成功后页面的变化,并结合art-template模板引擎,可以按照以下步骤进行: 1. 确保已安装Node.js和MySQL,并通过npm安装art-template和mysql模块。 2. 在MySQL中创建一个用户表,包含用户名和密码等字段。 3. 在Node.js中创建一个服务器,监听特定端口。 4. 创建路由来处理不同的请求,包括登录页面和登录请求。 5. 使用art-template模板引擎来渲染登录页面,包括表单和错误提示。 6. 当用户提交登录表单时,通过post请求来处理登录请求。 7. 在登录请求的处理函数中,通过向数据库查询匹配的用户名和密码来验证用户的凭据。 8. 如果验证成功,将用户信息保存在session或cookie中,以便在后续页面中使用。 9. 根据验证结果来渲染不同的页面,如登录成功后的主页或登录失败后的错误页面。 10. 在主页上,可以根据用户的登录状态,显示不同的内容。 以上是一个简单的示例,具体的实现过程会涉及更多细节和逻辑,例如密码加密、用户权限控制等。这样,当用户成功登录后,页面会根据登录状态和用户信息的变化而发生变化。使用art-template模板引擎能够方便地渲染页面,并通过与MySQL数据库进行交互来进行用户验证和数据查询操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值