vue3.0将后端返回的word文件流转换为pdf并导出+html2pdf.js将页面导出为pdf

news/2025/2/26 11:09:41

实现思路

1.将Word文档转换为HTML:mammoth.js,它可以将.docx文件转换为HTML
2.将HTML转换为PDF:使用html2html" title=pdf>pdf.js将HTML转换为PDF

如果想要相同的效果,也可以把前端页面直接导出转换为html" title=pdf>pdf: 运用的插件:html2html" title=pdf>pdf.js

后端文件流导出接口返回:
在这里插入图片描述
首先我们要安装mammoth.js和html2html" title=pdf>pdf.js

npm install mammoth jshtml" title=pdf>pdf

代码:

// 引入
import mammoth from "mammoth";
import html2html" title=pdf>pdf from "html2html" title=pdf>pdf.js";
 <el-button type="primary" :loading="isLoading" @click="healthExport">
     导出
  </el-button>
async function healthExport() {
  try {
    await signatureApi
      .onReportWord({
        id: selectedData.value[0].id,
        signId: selectedData.value[0].healthyId,
      })
      .then((res) => {
        if (res) {
          const reader = new FileReader();
          reader.onload = (e) => {
            const arrayBuffer = res;
            mammoth
              .convertToHtml({ arrayBuffer: arrayBuffer })
              .then((result) => {
                const htmlContent = result.value; // 这是转换后的HTML内容
                // 将HTML添加到页面的一个隐藏div中
                var div = document.createElement("div");
                // 可以给html" title=pdf>pdf自定义添加样式(将html" title=word>word提取html再转换成html" title=pdf>pdf导出这时候html" title=pdf>pdf是没有样式的)
                const css = `<style>body { p { text-align:center;font-size:20px;soild  }</style>`;
                div.innerHTML = `<html>${css}${htmlContent}</html>`;
                document.body.appendChild(div);

                // 使用html2html" title=pdf>pdf.js将HTML转换为PDF
                html2html" title=pdf>pdf()
                  .from(div)
                  .set({
                    html2canvas: { scale: 2 },
                    jsPDF: { format: "a4", orientation: "portrait" },
                    css: css,
                  })
                  .save("document.html" title=pdf>pdf");

                // 清理 - 移除添加的div
                document.body.removeChild(div);
              })
              .catch((err) => {
                console.error(err);
              });
          };
          reader.readAsArrayBuffer(res);
        }
      });
  } catch (error) {
    console.error("err", error);
  }
}

第二种前端思路:把前端页面直接导出转换为html" title=pdf>pdf
页面:点击导出直接导出为html" title=pdf>pdf
在这里插入图片描述

 <el-button type="primary" :loading="isLoading" @click="handleExport">
     导 出
  </el-button>
  
function handleExport() {
  html2html" title=pdf>pdf().set({
    html2canvas: { scale: 3 }, // 增加缩放比例以提高清晰度
    jsPDF: { unit: 'in', format: 'a4', orientation: 'portrait' } // 设置单位和纸张大小
}).from(document.getElementById('html" title=pdf>pdfRef')).save('健康体检通知书.html" title=pdf>pdf');
}

http://www.niftyadmin.cn/n/5868603.html

相关文章

wordpress禁止系统自带搜索功能增强被恶意搜索轰炸(google有效)

源地址&#xff1a;wordpress禁止系统自带搜索功能增强被恶意搜索轰炸&#xff08;google有效&#xff09;-计算机技术网 wordpress禁止系统自带搜索功能增强被恶意搜索轰炸&#xff08;google有效&#xff09; 10分钟前 • SEO优化方法, wordpressCMS教学教程, 谷歌搜索优化…

全面屏手势导航栏适配

背景 识别是否全面屏手势场景&#xff0c;然后识别手势指示条是否显示&#xff0c;然后再获取指示条高度 由于全面屏手势是Android9开始出现的&#xff0c;所以验证需要从Android9的机型测到最新的15看时机效果。 是否全面屏手势判断 public static boolean isGestureNavMod…

k8s集群3主5从高可用架构(kubeadm方式安装k8s)

关键步骤说明 环境准备阶段 系统更新&#xff1a;所有节点执行yum/apt update确保软件包最新时间同步&#xff1a;通过ntpdate time.windows.com或部署NTP服务器网络规划&#xff1a;明确划分Service网段&#xff08;默认10.96.0.0/12&#xff09;和Pod网段&#xff08;如Flann…

C++STL---<cstddef>

C 头文件 <cstddef> 详解 <cstddef> 是 C 标准库中与 内存布局和指针运算 密切相关的头文件&#xff0c;主要提供基础类型定义和宏操作。 核心类型定义 1. size_t 用途&#xff1a;表示对象大小的无符号整数类型特性&#xff1a; 保证足够大以容纳系统最大可能对…

【AIGC】使用Python实现科大讯飞语音服务ASR转录功能:完整指南

文章目录 讯飞ASR转写API完整指南1. 引言2. 讯飞ASR API介绍3. API参数说明3.1 认证参数3.2 上传参数3.3 查询结果参数3.4 orderResult 字段3.5 Lattice 字段3.6 json_1best 字段3.7 st 字段 4. Python代码实现4.1 生成签名4.2 上传音频文件4.3 获取转写结果4.4 解析转写结果 5…

10. docker nginx官方镜像使用方法

本文介绍docker nginx官方镜像使用方法&#xff0c;因为第一次用&#xff0c;在加上对docker也不是很熟&#xff0c;中间踩了一些坑&#xff0c;为了避免下一次用又踩坑&#xff0c;因此记录如下&#xff0c;也希望能够帮到其它小伙伴。 官方镜像页面&#xff1a;https://hub.d…

23种设计模式之《代理模式(Proxy)》在c#中的应用及理解

程序设计中的主要设计模式通常分为三大类&#xff0c;共23种&#xff1a; 1. 创建型模式&#xff08;Creational Patterns&#xff09; 单例模式&#xff08;Singleton&#xff09;&#xff1a;确保一个类只有一个实例&#xff0c;并提供全局访问点。 工厂方法模式&#xff0…

java面试题(一年工作经验)的心得

看面试题 正常人第一步肯定都会看面试题&#xff0c;我也不例外&#xff0c;在看的过程中&#xff0c;我发现有些文章写的不错&#xff0c;对我帮助不小值得推荐&#xff0c;如下&#xff1a; Java面试题全集&#xff08;上&#xff09; 很多基础的东西&#xff0c;建议先看。…