前端实现将当前页面内容下载成图片(图片可做到高清画质)

插件背景:

html2canvas可以把你想要转变的元素变为图片,使用file-saver下载图片。

1、安装html2canvas、file-saver

npm install html2canvas

npm install file-saver --save

2、在Vue组件中引入并使用html2canvas、file-saver

import html2canvas from 'html2canvas'
import { saveAs } from 'file-saver'

3、点击按钮调用html2canvas相关方法进行下载

示例代码如下:

注意:

1、过度增加 scale 可能会导致生成的图片质量提高但文件大小增加,并可能对性能产生负面影响。因此,应该根据实际需求和页面内容来选择合适的 scale 值。

2、要生成图片的dom中部分样式属性转化成图片后无法体现,如box-shadow属性,生成后的图片将丢失这个属性样式,因此我们需要使用border属性来实现边框样式让生成的图片内容显示边框样式。

<el-button :loading="loading" @click.stop="saveImg">下载</el-button>

saveImg() {
      this.loading = true
      const content = this.$refs.screenshot // 要下载成图片的dom
      if (!content) {
        this.loading = false
        return
      }
      html2canvas(content, {
        scale: 2, // 放大倍数,支持小数,可以控制清晰度
        letterRendering: true,
        backgroundColor: '#F2F4F3',
        height: this.$refs.screenshot.clientHeight - 70 // 要转化为图片下载的dom 高度
      }).then((canvas) => {
        canvas.toBlob(blob => {
          this.loading = false
          saveAs(blob, `Oferta para ${this.infoItem?.candidateName} .png`)
        }, 'image/png')
      })
    }

4、效果

触发下载方法后浏览器会提示下载成功,见下图:

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这个任务可以分以下几个步骤来实现: 1. 通过Java代码实现上传图片功能,可以使用Java的HTTPURLConnection类来完。首先需要构造一个HTTP POST请求,然后将图片数据作为请求体发送到服务器。 ```java URL url = new URL(uploadUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"" + lineEnd); dos.writeBytes("Content-Type: " + mimeType + lineEnd); dos.writeBytes(lineEnd); InputStream is = new FileInputStream(file); byte[] buffer = new byte[8192]; int count; while ((count = is.read(buffer)) > 0) { dos.write(buffer, 0, count); } dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); dos.flush(); dos.close(); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // 上传功 } else { // 上传失败 } ``` 2. 使用Java的ImageIO类加载上传的图片,并将图片的分辨率进行4倍放大。 ```java BufferedImage image = ImageIO.read(new File(filePath)); int width = image.getWidth() * 4; int height = image.getHeight() * 4; BufferedImage result = new BufferedImage(width, height, image.getType()); Graphics2D g2d = result.createGraphics(); g2d.drawImage(image, 0, 0, width, height, null); g2d.dispose(); ``` 3. 将放大后的图片保存到电脑D盘。 ```java File output = new File("D:\\" + fileName); ImageIO.write(result, "png", output); ``` 完整代码如下: ```java import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import javax.imageio.ImageIO; public class ImageUploader { private static final String uploadUrl = "http://example.com/upload.php"; private static final String boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"; private static final String lineEnd = "\r\n"; private static final String twoHyphens = "--"; public static void main(String[] args) { String filePath = "C:\\example.png"; String mimeType = "image/png"; String fileName = new File(filePath).getName(); try { // 上传图片 URL url = new URL(uploadUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"" + lineEnd); dos.writeBytes("Content-Type: " + mimeType + lineEnd); dos.writeBytes(lineEnd); InputStream is = new FileInputStream(filePath); byte[] buffer = new byte[8192]; int count; while ((count = is.read(buffer)) > 0) { dos.write(buffer, 0, count); } dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); dos.flush(); dos.close(); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // 上传功 // 读取上传的图片并放大 BufferedImage image = ImageIO.read(conn.getInputStream()); int width = image.getWidth() * 4; int height = image.getHeight() * 4; BufferedImage result = new BufferedImage(width, height, image.getType()); Graphics2D g2d = result.createGraphics(); g2d.drawImage(image, 0, 0, width, height, null); g2d.dispose(); // 保存放大后的图片到D盘 File output = new File("D:\\" + fileName); ImageIO.write(result, "png", output); } else { // 上传失败 System.out.println("Upload failed with response code " + responseCode); } } catch (Exception e) { e.printStackTrace(); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

唐小亭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值