经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » XHTML » 查看文章
html转pdf截图保存功能的实现_HTML/Xhtml
来源:jb51  时间:2020/12/22 17:41:14  对本文有异议

使用技术

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.  
  17. //格式化图片base64编码转换为byte文件流
  18. function convertBase64UrlToBlob(urlData){
  19. //去掉url的头,并转换为byte
  20. var bytes=window.atob(urlData.split(',')[1]);
  21. //处理异常,将ascii码小于0的转换为大于0
  22. var ab = new ArrayBuffer(bytes.length);
  23. var ia = new Uint8Array(ab);
  24. for (var s = 0;s<bytes.length;s++){
  25. ia[s] = bytes.charCodeAt(s);
  26. }
  27. return new Blob( [ab] , {type : 'image/png'});
  28. }
  29. <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.  
  12. FileImageOutputStream imageOutput = new FileImageOutputStream(new File(imagePath));//打开输入流
  13. imageOutput.write(fileByte, 0, fileByte.length);//生成本地图片文件
  14. imageOutput.close();
  15.  
  16. PdfWriter.getInstance(document, new FileOutputStream(filePath)); //itextpdf文件
  17. document.open();
  18. document.add(new Paragraph("JUST TEST ..."));
  19. Image image = Image.getInstance(imagePath); //itext-pdf-image
  20. float heigth = image.getHeight();
  21. float width = image.getWidth();
  22. int percent = getPercent2(heigth, width); //按比例缩小图片
  23. image.setAlignment(Image.MIDDLE);
  24. image.scalePercent(percent+3);
  25. document.add(image);
  26. document.close();
  27.  
  28. }catch (DocumentException de) {
  29. System.err.println(de.getMessage());
  30. }
  31. catch (Exception e) {
  32. e.printStackTrace();
  33.  
  34. }
  35. }
  36.  
  37. private static int getPercent2(float h, float w) {
  38. int p = 0;
  39. float p2 = 0.0f;
  40. p2 = 530 / w * 100;
  41. p = Math.round(p2);
  42. return p;
  43. }

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.  
  15. import javax.imageio.stream.FileImageOutputStream;
  16. import javax.servlet.http.HttpServletResponse;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.util.Map;

4 前端截图,访问后端接口,保存截图文件到本地为pdf或者其他格式的文件。

 有兴趣的同学可以把后端改为下载文件到本地

5 项目源码地址

https://github.com/zhangjy520/learn_java/tree/master/boot 

到此这篇关于html转pdf截图保存功能的实现的文章就介绍到这了,更多相关html转pdf截图保存内容请搜索w3xue以前的文章或继续浏览下面的相关文章,希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号