[前端] html转pdf截图保存功能的实现

2073 0
黑夜隐士 2022-10-19 16:02:23 | 显示全部楼层 |阅读模式
使用技术
itext.jar  : 将byte文件输入流转换为图片,pdf等
html2canvas.js :将html页面区域截图为base64编码的图片资源
java+js
1. 准备资源
itext.jar
www.baidu.com
html2canvas.js
www.baidu.com
2.前端代码:
  1. //进行截图操作,document.querySelector("body") 为要截图的区域
  2.      function test() {
  3.             html2canvas(document.querySelector("body"), {
  4.                 onrendered: function (canvas) {
  5.                     var dataUrl = canvas.toDataURL('image/png');
  6.                     var formData = new FormData(); //模拟表单对象
  7.                     formData.append("imgData", convertBase64UrlToBlob(dataUrl)); //写入数据
  8.                     var xhr = new XMLHttpRequest(); //数据传输方法
  9.                     xhr.open("POST", "http://localhost:8080/pdf"); //配置传输方式及地址
  10.                     xhr.send(formData);
  11.                     xhr.onreadystatechange = function () { //回调函数
  12.                     };
  13.                 }
  14.             });
  15.         }
  16.         //格式化图片base64编码转换为byte文件流
  17.         function convertBase64UrlToBlob(urlData){
  18.             //去掉url的头,并转换为byte
  19.             var bytes=window.atob(urlData.split(',')[1]);
  20.             //处理异常,将ascii码小于0的转换为大于0
  21.             var ab = new ArrayBuffer(bytes.length);
  22.             var ia = new Uint8Array(ab);
  23.             for (var s = 0;s<bytes.length;s++){
  24.                 ia[s] = bytes.charCodeAt(s);
  25.             }
  26.             return new Blob( [ab] , {type : 'image/png'});
  27.         }
  28.         <body onclick="test()">//调用截图方法即可
复制代码
3.后端代码:
  1. @RequestMapping(value = "/pdf",method = RequestMethod.POST)
  2.     public void test(MultipartHttpServletRequest request, HttpServletResponse response) throws IOException {
  3.         String filePath = "D:\\blog\\exportPdf2.pdf";
  4.         String imagePath = "D:\\blog\\exportImg2.png";
  5.         Document document = new Document();
  6.         try{
  7.             Map getMap = request.getFileMap();
  8.             MultipartFile mfile = (MultipartFile) getMap.get("imgData"); //获取数据
  9.             InputStream file = mfile.getInputStream();
  10.             byte[] fileByte = FileCopyUtils.copyToByteArray(file);
  11.             FileImageOutputStream imageOutput = new FileImageOutputStream(new File(imagePath));//打开输入流
  12.             imageOutput.write(fileByte, 0, fileByte.length);//生成本地图片文件
  13.             imageOutput.close();
  14.             PdfWriter.getInstance(document, new FileOutputStream(filePath)); //itextpdf文件
  15.             document.open();
  16.             document.add(new Paragraph("JUST TEST ..."));
  17.             Image image = Image.getInstance(imagePath); //itext-pdf-image
  18.             float heigth = image.getHeight();
  19.             float width = image.getWidth();
  20.             int percent = getPercent2(heigth, width);  //按比例缩小图片
  21.             image.setAlignment(Image.MIDDLE);
  22.             image.scalePercent(percent+3);
  23.             document.add(image);
  24.             document.close();
  25.         }catch (DocumentException de) {
  26.             System.err.println(de.getMessage());
  27.         }
  28.         catch (Exception e) {
  29.             e.printStackTrace();
  30.         }
  31.     }
  32.     private static int getPercent2(float h, float w) {
  33.         int p = 0;
  34.         float p2 = 0.0f;
  35.         p2 = 530 / w * 100;
  36.         p = Math.round(p2);
  37.         return p;
  38.     }
复制代码
4 包名
  1. import com.itextpdf.text.Document;
  2. import com.itextpdf.text.DocumentException;
  3. import com.itextpdf.text.Image;
  4. import com.itextpdf.text.Paragraph;
  5. import com.itextpdf.text.pdf.PdfWriter;
  6. import org.springframework.boot.SpringApplication;
  7. import org.springframework.boot.autoconfigure.SpringBootApplication;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.util.FileCopyUtils;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.multipart.MultipartFile;
  13. import org.springframework.web.multipart.MultipartHttpServletRequest;
  14. import javax.imageio.stream.FileImageOutputStream;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.util.Map;
复制代码
4 前端截图,访问后端接口,保存截图文件到本地为pdf或者其他格式的文件。
有兴趣的同学可以把后端改为下载文件到本地
5 项目源码地址
https://github.com/zhangjy520/learn_java/tree/master/boot
到此这篇关于html转pdf截图保存功能的实现的文章就介绍到这了,更多相关html转pdf截图保存内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章,希望大家以后多多支持中国红客联盟!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行