首页 > 解决方案 > 将 docx 文件转换为 PDF (vue.js)

问题描述

我在我的 vue.js 项目中加载了一个 docx 文件并对其进行了更改,现在我尝试将其转换为 PFD 文件并将其下载到用户的计算机上。我该怎么做 ?

(我通过 docxtemplater 更改的新 docx 文件不会保存在项目中,而是另存为变量,就像您在代码中看到的那样)。

<template>
<div>
    <button @click="renderDoc">
       Render docx template
    </button>
</div>
</template>

<script>
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("../templates/template.docx", function(
        error,
        content
      ) {      
        var zip = new PizZip(content);
        var doc = new Docxtemplater().loadZip(zip);

        doc.setData({
          first_name: "John",
          last_name: "Doe",
          phone: "0652455478",         
        });
        try {
          //change the doc file content
          doc.render();  
        } 
        catch (error) {         
          throw error;
        }
        var out = doc.getZip().generate({
          type: "blob",
          mimeType:
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        }); //Output the document using Data-URI
        saveAs(out, "output.docx");   
        
      });
    }
  },

};

</script>

标签: javascriptnode.jsvue.jspdfdocx

解决方案


推荐阅读