js,以word模版生成word

1. 以word模版生成word

模块:docxtemplater

不仅可以处理word(免费):也可excel(收费)、html(收费)、等等

官网文档:https://docxtemplater.com/docs/get-started-browser/

官网在线演示:https://docxtemplater.com/demo/#simple

1.1 浏览器版本写法:

(0) 定义模版 

(1) 安装模块
docxtemplate、file-saver、pizzip
(2) 编写

官方例子写法:https://stackblitz.com/edit/vuejs-docxtemplater-example?file=button.component.js

官方例子代码:

import Docxtemplater from "docxtemplater";
import PizZip from "pizzip";
import PizZipUtils from "pizzip/utils/index.js";
import { saveAs } from "file-saver";

function loadFile(url, callback) {
  PizZipUtils.getBinaryContent(url, callback);
}

export default {
  methods: {
    renderDoc() {
      loadFile("https://docxtemplater.com/tag-example.docx", function(
        error,
        content
      ) {
        if (error) {
          throw error;
        }
        const zip = new PizZip(content);
        const doc = new Docxtemplater(zip, { paragraphLoop: true, linebreaks: true });
        doc.setData({
          first_name: "John",
          last_name: "Doe",
          phone: "0652455478",
          description: "New Website"
        });
        try {
          // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
          doc.render();
        } catch (error) {
          // The error thrown here contains additional information when logged with JSON.stringify (it contains a properties object containing all suberrors).
          function replaceErrors(key, value) {
            if (value instanceof Error) {
              return Object.getOwnPropertyNames(value).reduce(function(
                error,
                key
              ) {
                error[key] = value[key];
                return error;
              },
              {});
            }
            return value;
          }
          console.log(JSON.stringify({ error: error }, replaceErrors));

          if (error.properties && error.properties.errors instanceof Array) {
            const errorMessages = error.properties.errors
              .map(function(error) {
                return error.properties.explanation;
              })
              .join("\n");
            console.log("errorMessages", errorMessages);
            // errorMessages is a humanly readable message looking like this :
            // 'The tag beginning with "foobar" is unopened'
          }
          throw error;
        }
        const out = doc.getZip().generate({
          type: "blob",
          mimeType:
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        });
        // Output the document using Data-URI
        saveAs(out, "output.docx");
      });
    }
  },

  template: `
    <button @click="renderDoc">
       Render docx template
    </button>
  `
};
View Code

 

1.2 nodejs写法:

看官方文档。

 
2. word转pdf
模块:libreoffice-convert(仅nodejs可用)
介绍及写法:https://www.npmjs.com/package/libreoffice-convert
 

 

posted @ 2022-09-26 17:16  zezhou222  阅读(776)  评论(0编辑  收藏  举报