ChatGPT解答:JavaScript保存当前网页页面图片为pdf文件或者word文件,前端用vue2,给出详细的方案和代码

news2024/10/6 6:01:39

ChatGPT解答:JavaScript保存当前网页页面图片为pdf文件或者word文件,前端用vue2,给出详细的方案和代码

 

 

ChatGPTDemo

Based on OpenAI API (gpt-3.5-turbo).

JavaScript保存当前网页页面图片为pdf文件或者word文件,前端用vue2,给出详细的方案和代码,保存当前网页页面图片为pdf文件或者word文件,可以通过第三方库实现。

  1. 使用jsPDF库保存为pdf文件

jsPDF是一个用于生成PDF文件的JavaScript库。它可以在客户端浏览器中生成PDF文件,也可以在服务器端使用Node.js生成PDF文件。

安装jsPDF库:

npm install jspdf --save

使用jsPDF库生成pdf文件:

import jsPDF from 'jspdf'

// 获取页面元素
const element = document.getElementById('pdf-content')

// 将元素转换为canvas
html2canvas(element).then(canvas => {
  // 获取canvas宽高
  const contentWidth = canvas.width
  const contentHeight = canvas.height

  // 将canvas转换为图片数据
  const imgData = canvas.toDataURL('image/png')

  // 设置pdf文件尺寸
  const pdfWidth = contentWidth / 2.75
  const pdfHeight = contentHeight / 2.75

  // 创建pdf实例
  const pdf = new jsPDF('p', 'mm', [pdfWidth, pdfHeight])

  // 添加图片到pdf文件
  pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight)

  // 保存pdf文件
  pdf.save('filename.pdf')
})
  1. 使用html-docx-js库保存为word文件

html-docx-js是一个用于将HTML转换为Microsoft Word文档的JavaScript库。

安装html-docx-js库:

npm install html-docx-js --save

使用html-docx-js库生成word文件:

import htmlDocx from 'html-docx-js'

// 获取页面元素
const element = document.getElementById('word-content')

// 将元素转换为html
const htmlContent = element.outerHTML

// 将html转换为word文档
const wordContent = htmlDocx.asBlob(htmlContent)

// 保存word文件
saveAs(wordContent, 'filename.docx')

注意:需要引入FileSaver库才能使用saveAs方法保存文件。

完整代码如下:

<template>
  <div>
    <div id="pdf-content">
      <!-- 页面内容 -->
    </div>
    <button @click="savePdf">保存为pdf文件</button>

    <div id="word-content">
      <!-- 页面内容 -->
    </div>
    <button @click="saveWord">保存为word文件</button>
  </div>
</template>

<script>
import jsPDF from 'jspdf'
import htmlDocx from 'html-docx-js'
import FileSaver from 'file-saver'

export default {
  methods: {
    savePdf() {
      // 获取页面元素
      const element = document.getElementById('pdf-content')

      // 将元素转换为canvas
      html2canvas(element).then(canvas => {
        // 获取canvas宽高
        const contentWidth = canvas.width
        const contentHeight = canvas.height

        // 将canvas转换为图片数据
        const imgData = canvas.toDataURL('image/png')

        // 设置pdf文件尺寸
        const pdfWidth = contentWidth / 2.75
        const pdfHeight = contentHeight / 2.75

        // 创建pdf实例
        const pdf = new jsPDF('p', 'mm', [pdfWidth, pdfHeight])

        // 添加图片到pdf文件
        pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight)

        // 保存pdf文件
        pdf.save('filename.pdf')
      })
    },
    saveWord() {
      // 获取页面元素
      const element = document.getElementById('word-content')

      // 将元素转换为html
      const htmlContent = element.outerHTML

      // 将html转换为word文档
      const wordContent = htmlDocx.asBlob(htmlContent)

      // 保存word文件
      FileSaver.saveAs(wordContent, 'filename.docx')
    }
  }
}
</script>

加入新功能:点击按钮后,用户选择保存路径后,再保存

要实现在用户选择保存路径后再保存文件,可以使用<input type="file">元素来让用户选择保存路径,然后再将文件保存到用户选择的路径中。

修改代码如下:

<template>
  <div>
    <div id="pdf-content">
      <!-- 页面内容 -->
    </div>
    <button @click="savePdf">保存为pdf文件</button>

    <div id="word-content">
      <!-- 页面内容 -->
    </div>
    <button @click="saveWord">保存为word文件</button>

    <input ref="fileInput" type="file" style="display: none" @change="saveFile">
  </div>
</template>

<script>
import jsPDF from 'jspdf'
import htmlDocx from 'html-docx-js'
import FileSaver from 'file-saver'

export default {
  methods: {
    savePdf() {
      // 获取页面元素
      const element = document.getElementById('pdf-content')

      // 将元素转换为canvas
      html2canvas(element).then(canvas => {
        // 获取canvas宽高
        const contentWidth = canvas.width
        const contentHeight = canvas.height

        // 将canvas转换为图片数据
        const imgData = canvas.toDataURL('image/png')

        // 设置pdf文件尺寸
        const pdfWidth = contentWidth / 2.75
        const pdfHeight = contentHeight / 2.75

        // 创建pdf实例
        const pdf = new jsPDF('p', 'mm', [pdfWidth, pdfHeight])

        // 添加图片到pdf文件
        pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight)

        // 显示文件选择框
        this.$refs.fileInput.click()

        // 将pdf文件保存到用户选择的路径中
        this.fileContent = pdf.output('blob')
      })
    },
    saveWord() {
      // 获取页面元素
      const element = document.getElementById('word-content')

      // 将元素转换为html
      const htmlContent = element.outerHTML

      // 将html转换为word文档
      const wordContent = htmlDocx.asBlob(htmlContent)

      // 显示文件选择框
      this.$refs.fileInput.click()

      // 将word文件保存到用户选择的路径中
      this.fileContent = wordContent
    },
    saveFile() {
      // 获取用户选择的文件
      const file = this.$refs.fileInput.files[0]

      // 如果用户取消选择,则返回
      if (!file) {
        return
      }

      // 保存文件到用户选择的路径中
      FileSaver.saveAs(this.fileContent, file.name)
    }
  }
}
</script>

修改后,点击保存按钮后会弹出文件选择框,用户选择保存路径后再保存文件。

完整的vue文件的内容

下面是完整的Vue文件的内容:

<template>
  <div>
    <div id="pdf-content">
      <!-- 页面内容 -->
    </div>
    <button @click="savePdf">保存为pdf文件</button>

    <div id="word-content">
      <!-- 页面内容 -->
    </div>
    <button @click="saveWord">保存为word文件</button>

    <input ref="fileInput" type="file" style="display: none" @change="saveFile">
  </div>
</template>

<script>
import jsPDF from 'jspdf'
import htmlDocx from 'html-docx-js'
import FileSaver from 'file-saver'

export default {
  data() {
    return {
      fileContent: null
    }
  },
  methods: {
    savePdf() {
      // 获取页面元素
      const element = document.getElementById('pdf-content')

      // 将元素转换为canvas
      html2canvas(element).then(canvas => {
        // 获取canvas宽高
        const contentWidth = canvas.width
        const contentHeight = canvas.height

        // 将canvas转换为图片数据
        const imgData = canvas.toDataURL('image/png')

        // 设置pdf文件尺寸
        const pdfWidth = contentWidth / 2.75
        const pdfHeight = contentHeight / 2.75

        // 创建pdf实例
        const pdf = new jsPDF('p', 'mm', [pdfWidth, pdfHeight])

        // 添加图片到pdf文件
        pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight)

        // 显示文件选择框
        this.$refs.fileInput.click()

        // 将pdf文件保存到用户选择的路径中
        this.fileContent = pdf.output('blob')
      })
    },
    saveWord() {
      // 获取页面元素
      const element = document.getElementById('word-content')

      // 将元素转换为html
      const htmlContent = element.outerHTML

      // 将html转换为word文档
      const wordContent = htmlDocx.asBlob(htmlContent)

      // 显示文件选择框
      this.$refs.fileInput.click()

      // 将word文件保存到用户选择的路径中
      this.fileContent = wordContent
    },
    saveFile() {
      // 获取用户选择的文件
      const file = this.$refs.fileInput.files[0]

      // 如果用户取消选择,则返回
      if (!file) {
        return
      }

      // 保存文件到用户选择的路径中
      FileSaver.saveAs(this.fileContent, file.name)
    }
  }
}
</script>

注意:需要在项目中安装jspdfhtml-docx-jsfile-saver库。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/385746.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Python 操作Redis

在 Python中我们使用 redis库来操作 Redis数据库。Redis数据库的使用命令这里就不介绍了。 需要安装 redis库。检查是否安装redis&#xff1a; pip redis 如果未安装&#xff0c;使用 pip命令安装 redis。 pip install redis #安装最新版本 一、Redis连接 Redis提供两个类 Re…

CEC2021:鱼鹰优化算法(Osprey optimization algorithm,OOA)求解CEC2021(提供MATLAB代码

一、鱼鹰优化算法简介 鱼鹰优化算法&#xff08;Osprey optimization algorithm&#xff0c;OOA&#xff09;由Mohammad Dehghani 和 Pavel Trojovsk于2023年提出&#xff0c;其模拟鱼鹰的捕食行为。 鱼鹰是鹰形目、鹗科、鹗属的仅有的一种中型猛禽。雌雄相似。体长51-64厘米…

2023年“楚怡杯“湖南省职业院校技能竞赛“网络安全”竞赛任务书

2023年“楚怡杯“湖南省职业院校技能竞赛“网络安全”竞赛任务书 一、竞赛时间 总计&#xff1a;360分钟 竞赛阶段竞赛阶段 任务阶段 竞赛任务 竞赛时间 分值 A模块 A-1 登录安全加固 180分钟 200分 A-2 本地安全策略配置 A-3 流量完整性保护 A-4 事件监控 …

技术官方文档中的代码是用什么展示的?代码高亮插件总结

****内容预警****菜鸟教程***大佬绕道我们经常看到各种技术官方文档&#xff0c;有很多代码展示的区域&#xff0c;用于我们复制粘贴代码&#xff0c;比如vue 的官网当我们需要自己实现这么一个网站的时候&#xff0c;我就开始手忙脚乱&#xff0c;这到底是咋实现的&#xff1f…

如何使用ChatGPT快速构建一个网站模板?

欢迎来到令人兴奋的自然语言处理和机器学习世界&#xff01;今天&#xff0c;我们将探索 ChatGPT 的功能&#xff0c;它是由 OpenAI 公司开发的目前最先进的人工智能工具。当然&#xff0c;你也可以将其看作是一个智能机器人。ChatGPT 最令人印象深刻的功能之一是它能够根据简单…

如何获取或设置CANoe以太网网卡信息(SET篇)

CAPL提供了一系列函数用来操作CANoe网卡。但是,但是,首先需要明确一点,不管是获取网卡信息,还是设置网卡信息,只能访问CAPL程序所在的节点下的网卡,而不是节点所在的以太网通道下的所有网卡 关于第一张图中,Class节点下,有三个网卡:Ethernet1、VLAN 1.100、VLAN 1.200…

我的 System Verilog 学习记录(8)

引言 本文简单介绍 SystemVerilog 的接口。 前文链接&#xff1a; 我的 System Verilog 学习记录&#xff08;1&#xff09; 我的 System Verilog 学习记录&#xff08;2&#xff09; 我的 System Verilog 学习记录&#xff08;3&#xff09; 我的 System Verilog 学习记…

哪个牌子的蓝牙耳机音质好?公认音质最好的真无线耳机推荐

现如今&#xff0c;使用蓝牙耳机的人越来越多&#xff0c;更多的蓝牙耳机品牌出现在大众视野。哪个牌子的蓝牙耳机音质好&#xff1f;最近看到很多人问音质。都说蓝牙耳机的音质比不上有线耳机的音质&#xff0c;但经过那么多年的技术进步&#xff0c;蓝牙耳机在音质上也有着不…

CSS - 扫盲

文章目录1. 前言2. CSS2.1 css 的引入方式2.2 选择器2.3 CSS 常用属性2.3.1 字体属性2.3.2 文本属性2.3.3 背景属性2.4 圆角矩形2.5 元素的显示模式2.6 盒子模型2.7 弹性布局1. 前言 上文我们简单 将 HTML 过了一遍 &#xff0c; 知道了 HTML 知识表示页面的结构和内容 &#x…

【NLP相关】Transformer模型:从Seq2Seq到自注意力机制(Transformer原理、公式推导和案例)

❤️觉得内容不错的话&#xff0c;欢迎点赞收藏加关注&#x1f60a;&#x1f60a;&#x1f60a;&#xff0c;后续会继续输入更多优质内容❤️&#x1f449;有问题欢迎大家加关注私戳或者评论&#xff08;包括但不限于NLP算法相关&#xff0c;linux学习相关&#xff0c;读研读博…

逆向练习之 mingyue.exe wp

目录 一.查壳 二.主函数 三.operate函数 四.storage函数及4618和4620指针功能的解释 五.judge函数 六.求解flag 七.其他--ida字符识别问题 一.查壳 64位无壳 二.主函数 1.这里的pointer_4618和4620是两个相邻的八字节内存单元,其中4620是字符串链表表头head 2.puts和s…

自动驾驶规划 - 5次多项式拟合

简介 自动驾驶运动规划中会用到各种曲线&#xff0c;主要用于生成车辆的轨迹&#xff0c;常见的轨迹生成算法&#xff0c;如贝塞尔曲线&#xff0c;样条曲线&#xff0c;以及apollo EM Planner的五次多项式曲线&#xff0c;城市场景中使用的是分段多项式曲线&#xff0c;在EM …

springCloud之OAuth2

认证授权过程 在认证和授权的过程中涉及的三方包括&#xff1a; 1、服务提供方&#xff0c;用户使用服务提供方来存储受保护的资源&#xff0c;如照片&#xff0c;视频&#xff0c;联系人列表。 2、用户&#xff0c;存放在服务提供方的受保护的资源的拥有者。 3、客户端&am…

【spring】事务

概述 1、什么事务 事务是数据库操作最基本单元&#xff0c;逻辑上一组操作&#xff0c;要么都成功&#xff0c;如果有一个失败所有操 作都失败 2、事务四个特性&#xff08;ACID&#xff09; &#xff08;1&#xff09;原子性 &#xff08;2&#xff09;一致性 &#xff08;3…

java语言跨平台的特性:“一次编译,到处运行”

“一次编译&#xff0c;到处运行”是java语言跨平台的特性&#xff0c;平台指的是操作系统平台。 程序从源代码到运行的三个必经阶段&#xff1a;编码——编译——运行&#xff0c;调试。 首先编码阶段&#xff0c;需要编码语言是一个程序设计语言&#xff0c;而我们的java是程…

ChatGPT解答:python大批量读写ini文件时,性能很低,有什么解决方法吗,给出具体的思路和实例

ChatGPT解答&#xff1a; python大批量读写ini文件时&#xff0c;性能很低&#xff0c;有什么解决方法吗&#xff0c;给出具体的思路和实例 ChatGPTDemo Based on OpenAI API (gpt-3.5-turbo). python大批量读写ini文件时&#xff0c;性能很低&#xff0c;有什么解决方法吗&…

springboot工程搭建的几种方式

一、通过idea工具搭建&#xff0c;如下&#xff1a; 新建Project和Module&#xff0c;选择Spring initializr&#xff0c;点击Next&#xff0c;进入到如下页面填写 填写完后点击Next 到如下页面&#xff0c;根据你的具体所需&#xff0c;选择要使用的技术依赖 点击Next 点击FIn…

巾帼绽芬芳 一起向未来(中篇)

编者按&#xff1a;为了隆重纪念纪念“三八”国际妇女节113周年&#xff0c;快来与你全方位、多层次分享交流“三八”国际妇女节的前世今生。分上篇&#xff08;节日简介、节日发展和节日意义&#xff09;、中篇&#xff08;节日活动宗旨和世界各国庆祝方式&#xff09;和下篇&…

云原生之docker网络详解

云原生之docker网络详解一、相关概念1.2、CNM1.3、libnetwork二、实操2.1、docker network常用命令2.2、运行一个docker容器&#xff0c;查看CNM三个概念2.3、查看docker0在内核路由表上的记录2.4、查看网络列表2.5、网络隔离效果展示2.6、host驱动网络一、相关概念 1.1、网桥…

链表经典刷题--快慢指针与双指针

本篇总结链表解题思路----快慢指针&#xff0c;其实也就是双指针&#xff0c;这个快慢并不单纯指“快慢”&#xff0c;它更多的可以表示&#xff0c;速度快慢&#xff0c;距离长度&#xff0c;时间大小等等&#xff0c;用法很有趣也很独特&#xff0c;理解它的思想&#xff0c;…