printPage.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <link rel="stylesheet" href="../css/bootstrap.min.css">
  7. <link rel="stylesheet" href="../css/bootstrap-dialog.min.css">
  8. </head>
  9. <body id="mc">
  10. <div id="content" class="container-fluid">
  11. <canvas id="canvas" width="1490" height="10000" style="border:1px solid #d3d3d3;" ref="canvas"></canvas>
  12. </div>
  13. <script src="../js/jquery.min.js"></script>
  14. <script src="../js/bootstrap.min.js"></script>
  15. <script src="../js/bootstrap-dialog.min.js"></script>
  16. <script src="/static/JsBarcode/JsBarcode.all.js"></script>
  17. <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script>
  18. <script type="text/javascript">
  19. var content = new Vue({
  20. el: "#content",
  21. data: function() {
  22. return {
  23. img: new Image(), // 背景图片缓存
  24. context: {}, // canvas对象
  25. HEIGHT: 0,
  26. printPages: [{
  27. //纸张宽度
  28. paperWidth: 500,
  29. //纸张高度
  30. paperHeight: 300,
  31. //纸张间距
  32. paperMargin: 10,
  33. //打印元素
  34. printItems: [{
  35. //内容
  36. content: '内容1苏覅是否会第三方的',
  37. //隐藏内容
  38. hideTitle: '456',
  39. //格式(文本,条形码,二维码,EPC).
  40. displayType: '文本',
  41. //显示.
  42. enabled: true,
  43. //左边距
  44. left: 10,
  45. //上边距
  46. top: 10,
  47. //宽度
  48. width: 200,
  49. //高度
  50. height: 100,
  51. //码字 Code-128 Ean-13
  52. barCodeType: undefined
  53. }, {
  54. //内容
  55. content: '123456789',
  56. //隐藏内容
  57. hideTitle: '789',
  58. //格式(文本,条形码,二维码,EPC).
  59. displayType: '条形码',
  60. //显示.
  61. enabled: true,
  62. //左边距
  63. left: 10,
  64. //上边距
  65. top: 30,
  66. //宽度
  67. width: 200,
  68. //高度
  69. height: 100,
  70. //码字 Code-128 Ean-13
  71. barCodeType: 'CODE128'
  72. }, {
  73. //内容
  74. content: undefined,
  75. //隐藏内容
  76. hideTitle: undefined,
  77. //格式(文本,条形码,二维码,EPC).
  78. displayType: '二维码',
  79. //显示.
  80. enabled: true,
  81. //左边距
  82. left: 260,
  83. //上边距
  84. top: 30,
  85. //宽度
  86. width: 200,
  87. //高度
  88. height: 100,
  89. //码字 Code-128 Ean-13
  90. barCodeType: undefined
  91. }]
  92. },
  93. {
  94. //纸张宽度
  95. paperWidth: 500,
  96. //纸张高度
  97. paperHeight: 300,
  98. //纸张间距
  99. paperMargin: 10,
  100. //打印元素
  101. printItems: [{
  102. //内容
  103. content: '内容2',
  104. //隐藏内容
  105. hideTitle: '456',
  106. //格式(文本,条形码,二维码,EPC).
  107. displayType: '文本',
  108. //显示.
  109. enabled: true,
  110. //左边距
  111. left: 10,
  112. //上边距
  113. top: 10,
  114. //宽度
  115. width: 200,
  116. //高度
  117. height: 100,
  118. //码字 Code-128 Ean-13
  119. barCodeType: undefined
  120. }, {
  121. //内容
  122. content: '1234567890',
  123. //隐藏内容
  124. hideTitle: '789',
  125. //格式(文本,条形码,二维码,EPC).
  126. displayType: '条形码',
  127. //显示.
  128. enabled: true,
  129. //左边距
  130. left: 10,
  131. //上边距
  132. top: 30,
  133. //宽度
  134. width: 200,
  135. //高度
  136. height: 100,
  137. //码字 Code-128 Ean-13
  138. barCodeType: 'CODE128'
  139. }, {
  140. //内容
  141. content: undefined,
  142. //隐藏内容
  143. hideTitle: undefined,
  144. //格式(文本,条形码,二维码,EPC).
  145. displayType: '二维码',
  146. //显示.
  147. enabled: true,
  148. //左边距
  149. left: 260,
  150. //上边距
  151. top: 30,
  152. //宽度
  153. width: 200,
  154. //高度
  155. height: 100,
  156. //码字 Code-128 Ean-13
  157. barCodeType: undefined
  158. }]
  159. }
  160. ]
  161. }
  162. },
  163. methods: {
  164. draw: function() {
  165. var _self = this;
  166. var h = 0;
  167. var h1 = 0;
  168. if (_self.printPages != null && _self.printPages.length > 0) {
  169. for (var index = 0; index < _self.printPages.length; index++) {
  170. h += _self.printPages[index].paperMargin;
  171. h1 = h;
  172. //设置坐标,长宽
  173. this.rect(h,_self.printPages[index]);
  174. h += _self.printPages[index].paperHeight;
  175. var PrintItems = _self.printPages[index].printItems;
  176. if (PrintItems != null && PrintItems.length > 0) {
  177. for (var x = 0; x < PrintItems.length; x++) {
  178. var LEFT = 30 + PrintItems[x].left;
  179. var TOP = h1 + PrintItems[x].top;
  180. if (PrintItems[x].displayType == '文本') {
  181. this.content(PrintItems[x], LEFT, TOP);
  182. }
  183. if (PrintItems[x].displayType == '条形码') {
  184. this.barcode(PrintItems[x], LEFT, TOP);
  185. }
  186. if (PrintItems[x].displayType == '二维码') {
  187. this.QRcode(PrintItems[x], LEFT, TOP);
  188. }
  189. }
  190. }
  191. }
  192. }
  193. },
  194. rect: function(h,printPage) {
  195. //设置矩形坐标,长宽
  196. this.context.rect(30, h, printPage.paperWidth, printPage.paperHeight);
  197. this.context.stroke();
  198. },
  199. content: function(printItem, LEFT, TOP) {
  200. //添加文本
  201. this.context.font = "10px Georgia";
  202. this.context.fillText(printItem.content, LEFT, TOP);
  203. },
  204. barcode: function(printItem, LEFT, TOP) {
  205. //添加条形码
  206. var barcode = new Image;
  207. var str = printItem.content,
  208. options = {
  209. format: printItem.barCodeType,
  210. displayValue: true,
  211. fontSize: 18,
  212. height: 100
  213. };
  214. JsBarcode(barcode, str, options); //原生
  215. // 图片加载完成后执行
  216. barcode.onload = () => {
  217. this.context.drawImage(barcode, LEFT, TOP, printItem.width, printItem.height);
  218. }
  219. },
  220. QRcode: function(printItem, LEFT, TOP) {
  221. //添加二维码
  222. var img = new Image;
  223. // 获取图片地址
  224. img.src = "../image/template-logo.png";
  225. // 图片加载完成后执行
  226. img.onload = () => {
  227. this.context.drawImage(img, LEFT, TOP, printItem.width, printItem.height);
  228. }
  229. }
  230. },
  231. mounted: function() {
  232. var _self = this;
  233. const canvas = this.$refs.canvas;
  234. this.context = canvas.getContext('2d')
  235. if (_self.printPages != null && _self.printPages.length > 0) {
  236. for (var index = 0; index < _self.printPages.length; index++) {
  237. _self.HEIGHT += _self.printPages[index].paperMargin + _self.printPages[index].paperHeight;
  238. }
  239. }
  240. this.draw();
  241. }
  242. })
  243. </script>
  244. </body>
  245. </html>