>
截图保存或者html内容保存图片
2024-04-15 15:24
前端
  • 939
  • 527
  • 64
  • 51

把html内容或者某个div的内容保存图片  类似与截图

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="UTF-8">
  5.         <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.         <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.         <title>Document</title>
  8.         <script type='text/javascript' src='https://cyimt.net/html2canvas.js'></script>
  9.         <style>
  10.             *{
  11.                 padding0;
  12.                 margin0;
  13.             }
  14.             html, body{
  15.                 width100%;
  16.                 height100%;
  17.             }
  18.             .btn-down{
  19.                 position: fixed;
  20.                 top0;
  21.                 left0;
  22.                 width15.2083vw;
  23.                 height:5.5185vh;
  24.                 font-size40px;
  25.                 border-radius: 0.9259vh;
  26.                 color#fff;
  27.                 text-align: center;
  28.                 cursor: pointer;
  29.                 font-size: 5.3704vh;
  30.             }
  31.             .container{
  32.                 width100%;
  33.                 height100%;
  34.                 background: lightblue;
  35.                 display: flex;
  36.                 align-items: center;
  37.                 justify-content: center;
  38.             }
  39.             .container-inner{
  40.                 width80.7292vw;
  41.                 height30vh;
  42.                 background: pink;
  43.             }
  44.             .bookbox-inner-con {
  45.                 position: absolute;
  46.                 top42.3148vh;
  47.                 left23.75vw;
  48.                 right23.75vw;
  49.                 bottom31.0185vh;
  50.                 color#B99858;
  51.                 font-weight: bold;
  52.                 font-family: Source Han Sans CN;
  53.             }
  54.             .bookbox-inner-con .inner-con-txt {
  55.                 font-size: 3.7037vh;
  56.                 text-align: center;
  57.                 padding0 2.5vw;
  58.             }
  59.             .bookbox-inner-con .inner-con-ks {
  60.                 margin-top:7.3704vh;
  61.                 margin-bottom: 1.5741vh;
  62.                 text-align: right;
  63.                 font-size: 2.8704vh;
  64.            
  65.             }
  66.             .bookbox-inner-con .inner-con-time{
  67.                 text-align: right;
  68.                 font-size: 2.8704vh;
  69.            
  70.             }
  71.        
  72.       </style>
  73.     </head>
  74.     <body>
  75.         <div class="btn-down" onclick="saveCertificateBook()">下载</div>
  76.         <div class="container">
  77.             <div class="container-inner" id="farmImage">
  78.                 <div class="bookbox-inner-con">
  79.                     <div class="inner-con-txt">恭喜您在本次测试中获得“优秀”特此颁发证书以示鼓励</div>
  80.                     <div class="inner-con-ks">《测试》</div>
  81.                     <div class="inner-con-time">2022 年 10月 10日</div>
  82.                 </div>
  83.             </div>
  84.         </div>
  85.         <script>
  86.             function saveCertificateBook(){
  87.                 var date = new Date();
  88.                         const canvasID = document.getElementById("farmImage");
  89.                         const  rect=document.getElementById("farmImage").getBoundingClientRect()
  90.                          // 生成图片并上传到数据库保存
  91.                          html2canvas(canvasID, {
  92.                            scale0.5, // 数值越大生成的图片越清晰
  93.                            useCORS: true,
  94.                            allowTaint: true,
  95.                            y0// 滚动条高度修复
  96.                            x0,
  97.                            allowTaint: true//开启跨域
  98.                            useCORS: true,
  99.                            scrollX0,
  100.                            scrollY: rect.top, // 关键代码
  101.                            height: rect.height, // 加高度,避免截取不全
  102.                          }).then((canvas) => {
  103.                                     let dataURL = canvas.toDataURL("image/jpeg"); // 拿到数据流    
  104.                                     var aLink = document.createElement("a"); // 创建一个a标签
  105.                                     var blob = base64ToBlob(dataURL);
  106.                                     var event = document.createEvent("HTMLEvents");
  107.                                     event.initEvent("click"truetrue);
  108.                                     aLink.download = '证书' + date.getTime() + "." + blob.type.split("/")[1]; // 使用时间戳给文件命名
  109.                                     aLink.href = URL.createObjectURL(blob);
  110.                                     console.log(aLink.href )
  111.                                     aLink.click();
  112.                            })
  113.             }
  114.              // base64转Blob对象
  115.              function   base64ToBlob(code{
  116.                         var parts = code.split(";base64,");
  117.                         var contentType = parts[0].split(":")[1];
  118.                         var raw = window.atob(parts[1]);
  119.                         var rawLength = raw.length;
  120.                         var uint8Array = new Uint8Array(rawLength);
  121.                         for (var i = 0; i < rawLength; i++) {
  122.                             uint8Array[i] = raw.charCodeAt(i);
  123.                         }
  124.                         return new Blob([uint8Array], {type: contentType});
  125.                     }
  126.         </script>
  127.     </body>
  128. </html>


全部留言 ()
返回
顶部