qrcode.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. {extend name="common/common2" /}
  2. {block name="main"}
  3. <div class="row">
  4. <div class="col-sm-12">
  5. <div class="ibox float-e-margins">
  6. <div class="ibox-content">
  7. <div class="text-center">
  8. <div id="printer" class="text-center" style="width: 350px;height: 350px;padding-top:20px;margin: 0 auto;text-align: center;">
  9. <img src="{:url('Qrcode/qrcode',[],'')}?code={$code}" width="300" alt="">
  10. <p>{$info.title}</p>
  11. <p><br></p>
  12. </div>
  13. <a href="javascript:;" class="btn btn-sm btn-primary" onclick="print()">
  14. <i class="glyphicon glyphicon-print">打印</i>
  15. </a>
  16. <a href="javascript:;" class="btn btn-sm btn-primary" onclick="down()">
  17. <i class="fa fa-cloud-download">下载</i>
  18. </a>
  19. </div>
  20. </div>
  21. </div>
  22. </div>
  23. </div>
  24. {/block}
  25. {block name="script"}
  26. <script src="/static/browser.js"></script>
  27. <script src="/static/jquery.jqprint-0.3.js"></script>
  28. <script src="/static/html2canvas.min.js"></script>
  29. <script>
  30. function print(){
  31. $("#printer").jqprint();
  32. }
  33. function down() {
  34. var canvas2 = document.createElement("canvas");
  35. let _canvas = document.querySelector('#printer');
  36. var w = parseInt(window.getComputedStyle(_canvas).width);
  37. var h = parseInt(window.getComputedStyle(_canvas).height);
  38. //将canvas画布放大若干倍,然后盛放在较小的容器内,就显得不模糊了
  39. canvas2.width = w * 4;
  40. canvas2.height = h * 4;
  41. canvas2.style.width = w * 4 + "px";
  42. canvas2.style.height = h * 4 + "px";
  43. //可以按照自己的需求,对context的参数修改,translate指的是偏移量
  44. // var context = canvas.getContext("2d");
  45. // context.translate(0,0);
  46. var rect = $('#printer').get(0).getBoundingClientRect();//获取元素相对于视察的偏移量
  47. var context = canvas2.getContext("2d");
  48. context.scale(4, 4);
  49. // context.translate(rect.left, -rect.top);
  50. html2canvas(document.querySelector('#printer'), {
  51. // canvas: canvas2,
  52. //allowTaint : false,
  53. taintTest: true,
  54. useCORS: true,
  55. }).then(function (canvas) {
  56. var saveFile = function (data, filename) {
  57. var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
  58. save_link.href = data;
  59. save_link.download = filename;
  60. var event = document.createEvent('MouseEvents');
  61. event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  62. save_link.dispatchEvent(event);
  63. };
  64. //最终文件名
  65. var filename = '{$info.title}.png';
  66. saveFile(canvas.toDataURL(), filename);
  67. });
  68. }
  69. </script>
  70. {/block}