HiPrintService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace services\extend\printer;
  3. use common\helpers\StringHelper;
  4. use Yii;
  5. use yii\helpers\Json;
  6. use function services\extend\mb_strlen;
  7. use function services\extend\mb_substr;
  8. /**
  9. * 本地打印
  10. *
  11. * 官网:http://hiprint.io/
  12. *
  13. * Class HiPrintService
  14. * @package services\extend
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class HiPrintService
  18. {
  19. /**
  20. * 58 纸张
  21. *
  22. * @param $data
  23. * @return \array[][]|mixed
  24. * @throws \yii\base\InvalidConfigException
  25. */
  26. public function textByFiftyEight($data)
  27. {
  28. if (!is_array($data)) {
  29. return $data;
  30. }
  31. $width = 659;
  32. $fontSize = 43;
  33. $num = 0;
  34. $content = [];
  35. $content[] = " ";
  36. $content[] = "********** {$data['title']} **********";
  37. $content[] = str_repeat('.', 28);
  38. $content[] = "--- {$data['payType']} ---";
  39. $content[] = [
  40. 'options' => [
  41. 'title' => "打印时间: " . Yii::$app->formatter->asDatetime(time()),
  42. 'fontSize' => 35
  43. ]
  44. ];
  45. $content[] = [
  46. 'options' => [
  47. 'title' => "下单时间: " . $data['orderTime'],
  48. 'fontSize' => 35,
  49. ]
  50. ];
  51. $content[] = str_repeat('*', 11) . " 商品 " . str_repeat("*", 11);
  52. $productTable = [
  53. [
  54. ['title' => '商品名称'],
  55. ['title' => '数量'],
  56. ['title' => '金额'],
  57. ]
  58. ];
  59. foreach ($data['products'] as $product) {
  60. list($tmpMum, $title) = $this->effectiveLen($product['title']);
  61. $num += $tmpMum;
  62. $productTable[] = [
  63. ['title' => $title],
  64. ['title' => 'x' . $product['num']],
  65. ['title' => '¥' . $product['price']],
  66. ];
  67. }
  68. $content[] = [
  69. 'options' => [
  70. 'columns' => $productTable,
  71. 'textAlign' => 'center',
  72. 'tableBorder' => 'noBorder',
  73. 'tableHeaderBorder' => 'noBorder',
  74. 'tableHeaderCellBorder' => 'noBorder',
  75. 'tableBodyRowBorder' => 'noBorder',
  76. 'height' => count($productTable) * 80
  77. ],
  78. 'printElementType' => [
  79. 'type' => 'tableCustom',
  80. 'title' => '表格',
  81. ]
  82. ];
  83. $content[] = str_repeat('*', 28);
  84. // 价格
  85. $priceTable = [
  86. [
  87. ['title' => '商品总价'],
  88. ['title' => '¥' . $data['productOriginalMoney']]
  89. ]
  90. ];
  91. foreach ($data['marketingDetails'] as $marketingDetail) {
  92. $priceTable[] = [
  93. ['title' => $marketingDetail['marketing_name'],],
  94. ['title' => '-¥' . $marketingDetail['discount_money'],],
  95. ];
  96. }
  97. if ($data['pointMoney'] > 0) {
  98. $priceTable[] = [
  99. ['title' => '积分抵扣'],
  100. ['title' => '¥' . $data['pointMoney']]
  101. ];
  102. }
  103. if ($data['taxMoney'] > 0) {
  104. $priceTable[] = [
  105. ['title' => '发票税额'],
  106. ['title' => '¥' . $data['taxMoney']]
  107. ];
  108. }
  109. $priceTable[] = [
  110. ['title' => '配送费'],
  111. ['title' => '¥' . $data['shippingMoney']]
  112. ];
  113. $priceTable[] = [
  114. ['title' => '应付金额'],
  115. ['title' => '¥' . $data['payMoney']],
  116. ];
  117. $content[] = [
  118. 'options' => [
  119. 'columns' => $priceTable,
  120. 'tableBorder' => 'noBorder',
  121. 'tableHeaderBorder' => 'noBorder',
  122. 'tableHeaderCellBorder' => 'noBorder',
  123. 'tableBodyRowBorder' => 'noBorder',
  124. 'left' => 0,
  125. 'textAlign' => 'right',
  126. 'height' => count($priceTable) * 50
  127. ],
  128. 'printElementType' => [
  129. 'type' => 'tableCustom',
  130. 'title' => '表格'
  131. ]
  132. ];
  133. $content[] = str_repeat('.', 28);
  134. $content[] = [
  135. 'options' => [
  136. 'title' => "昵称: " . $data['nickname'],
  137. 'fontSize' => 35
  138. ]
  139. ];
  140. isset($data['receiverName']) && $content[] = [
  141. 'options' => [
  142. 'title' => "客户: " . $data['receiverName'],
  143. 'fontSize' => 35
  144. ]
  145. ];
  146. isset($data['receiverMobile']) && $content[] = [
  147. 'options' => [
  148. 'title' => "电话: " . StringHelper::hideStr($data['receiverMobile'], 3),
  149. 'fontSize' => 35
  150. ]
  151. ];
  152. isset($data['receiverAddress']) && $content[] = [
  153. 'options' => [
  154. 'title' => "地址: " . $data['receiverRegionName'] . $data['receiverAddress'],
  155. 'fontSize' => 35
  156. ]
  157. ];
  158. isset($data['buyerMessage']) && $content[] = [
  159. 'options' => [
  160. 'title' => "备注: " . (!empty($data['buyerMessage']) ? $data['buyerMessage'] : '无'),
  161. 'fontSize' => 35
  162. ]
  163. ];
  164. $content[] = str_repeat('.', 28);
  165. $content[] = "二维码";
  166. if (!empty($data['qr'])) {
  167. $content[] = [
  168. 'options' => [
  169. 'title' => $data['qr'],
  170. 'textType' => 'qrcode',
  171. 'textAlign' => 'center',
  172. 'left' => 129,
  173. 'height' => 200,
  174. 'width' => 210,
  175. ]
  176. ];
  177. } else {
  178. $content[] = [
  179. 'options' => [
  180. 'title' => $data['orderSn'],
  181. 'textType' => 'qrcode',
  182. 'textAlign' => 'center',
  183. 'left' => 129,
  184. 'height' => 400,
  185. 'width' => 400,
  186. ]
  187. ];
  188. }
  189. $content[] = str_repeat('.', 28);
  190. $content[] = "订单号";
  191. $content[] = [
  192. 'options' => [
  193. 'title' => $data['orderSn'],
  194. 'textType' => 'barcode',
  195. 'textAlign' => 'center',
  196. 'fontSize' => $fontSize,
  197. 'lineHeight' => 60,
  198. 'left' => 77,
  199. 'height' => 80,
  200. 'width' => 500,
  201. ]
  202. ];
  203. $content[] = " ";
  204. $content[] = str_repeat('*', 12) . " 完 " . str_repeat('*', 12);
  205. $printElements = [];
  206. $top = 20;
  207. foreach ($content as $key => $item) {
  208. if (is_array($item)) {
  209. !isset($item['options']['top']) && $item['options']['top'] = $top;
  210. if (empty($item['options']['textAlign'])) {
  211. !isset($item['options']['left']) && $item['options']['left'] = 30;
  212. }
  213. !isset($item['options']['width']) && $item['options']['width'] = $width;
  214. !isset($item['options']['height']) && $item['options']['height'] = 35;
  215. !isset($item['options']['lineHeight']) && $item['options']['lineHeight'] = 35;
  216. !isset($item['options']['fontWeight']) && $item['options']['fontWeight'] = $key == 0 ? 600 : 500;
  217. !isset($item['options']['fontSize']) && $item['options']['fontSize'] = $fontSize;
  218. !isset($item['printElementType']) && $item['printElementType'] = [
  219. 'title' => '文本',
  220. 'type' => 'text'
  221. ];
  222. } else {
  223. $item = [
  224. 'options' => [
  225. 'title' => $item,
  226. 'fontSize' => $fontSize,
  227. 'height' => 35,
  228. 'width' => $width,
  229. 'top' => $top,
  230. 'left' => 0,
  231. 'fontWeight' => $key == 0 ? 600 : 500,
  232. 'textAlign' => 'center',
  233. 'lineHeight' => 27,
  234. ],
  235. 'printElementType' => [
  236. 'title' => '文本',
  237. 'type' => 'text'
  238. ]
  239. ];
  240. }
  241. $top += $item['options']['height'] + 30;
  242. $printElements[] = $item;
  243. }
  244. return [
  245. 'panels' => [
  246. [
  247. 'index' => '0',
  248. 'paperHeader' => 10,
  249. 'paperFooter' => 10,
  250. 'height' => (int)($top / 2.7) + $num * 10,
  251. 'width' => 233,
  252. 'printElements' => $printElements
  253. ]
  254. ]
  255. ];
  256. }
  257. /**
  258. * @param $string
  259. * @return array
  260. */
  261. protected function effectiveLen($string)
  262. {
  263. $len = strlen($string);
  264. if ($len < 24) {
  265. return [0, $string];
  266. }
  267. $letter = [];
  268. for ($i = 0; $i < mb_strlen($string, 'UTF-8'); $i++) {
  269. $letter[] = mb_substr($string, $i, 1, 'UTF-8');
  270. }
  271. $num = 0;
  272. foreach ($letter as $key => $value) {
  273. if ($key > 0 && ($key % 6) == 0) {
  274. $num++;
  275. }
  276. }
  277. return [$num, $string];
  278. }
  279. public function test()
  280. {
  281. $str = '{"panels":[{"index":0,"height":297,"width":210,"paperHeader":49.5,"paperFooter":780,"printElements":[{"options":{"left":175.5,"top":10.5,"height":27,"width":259,"title":"HiPrint自定义模块打印插件","fontSize":19,"fontWeight":"600","textAlign":"center","lineHeight":26},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":60,"top":27,"height":13,"width":52,"title":"页眉线","textAlign":"center"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":25.5,"top":57,"height":705,"width":9,"fixed":true,"borderStyle":"dotted"},"printElementType":{"type":"vline"}},{"options":{"left":60,"top":61.5,"height":48,"width":87,"src":"/Content/assets/hi.png"},"printElementType":{"title":"图片","type":"image"}},{"options":{"left":153,"top":64.5,"height":39,"width":276,"title":"二维码以及条形码均采用svg格式打印。不同打印机打印不会造成失真。图片打印:不同DPI打印可能会导致失真,","fontFamily":"微软雅黑","textAlign":"center","lineHeight":18},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":457.5,"top":79.5,"height":13,"width":120,"title":"姓名","field":"name","testData":"古力娜扎","color":"#f00808","textDecoration":"underline","textAlign":"center"},"printElementType":{"title":"文本","type":"text"}},{"options":{"left":499.5,"top":120,"height":43,"width":51,"title":"123456789","textType":"qrcode"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":285,"top":130.5,"height":34,"width":175,"title":"123456789","fontFamily":"微软雅黑","textAlign":"center","textType":"barcode"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":60,"top":132,"height":19,"width":213,"title":"所有打印元素都可已拖拽的方式来改变元素大小","fontFamily":"微软雅黑","textAlign":"center","lineHeight":18},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":153,"top":189,"height":13,"width":238,"title":"单击元素,右侧可自定义元素属性","textAlign":"center","fontFamily":"微软雅黑"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":60,"top":190.5,"height":13,"width":51,"title":"横线","textAlign":"center"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":415.5,"top":190.5,"height":13,"width":164,"title":"可以配置各属性的默认值","textAlign":"center","fontFamily":"微软雅黑"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":60,"top":214.5,"height":10,"width":475.5},"printElementType":{"title":"横线","type":"hline"}},{"options":{"left":235.5,"top":220.5,"height":32,"width":342,"title":"自定义表格:用户可左键选中表头,右键查看可操作项,操作类似Excel,双击表头单元格可进行编辑。内容:title#field","fontFamily":"微软雅黑","textAlign":"center","lineHeight":15},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":156,"top":265.5,"height":13,"width":94,"title":"表头列大小可拖动","fontFamily":"微软雅黑","textAlign":"center"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":60,"top":265.5,"height":13,"width":90,"title":"红色区域可拖动","fontFamily":"微软雅黑","textAlign":"center"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":60,"top":285,"height":44,"width":511.5,"field":"table","columns":[[{"width":85.25,"colspan":1,"rowspan":1,"checked":true},{"width":85.25,"colspan":1,"rowspan":1,"checked":true},{"title":"姓名","field":"name","width":85.25,"align":"center","colspan":1,"rowspan":1,"checked":true,"columnId":"name"},{"width":85.25,"colspan":1,"rowspan":1,"checked":true},{"width":85.25,"colspan":1,"rowspan":1,"checked":true},{"width":85.25,"colspan":1,"rowspan":1,"checked":true}]]},"printElementType":{"title":"表格","type":"tableCustom"}},{"options":{"left":21,"top":346.5,"height":61.5,"width":15,"title":"装订线","lineHeight":18,"fixed":true,"contentPaddingTop":3.75,"backgroundColor":"#ffffff"},"printElementType":{"type":"text"}},{"options":{"left":225,"top":349.5,"height":13,"width":346.5,"title":"自定义模块:主要为开发人员设计,能够快速,简单,实现自己功能","textAlign":"center"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":60,"top":370.5,"height":18,"width":79,"title":"配置项表格","textAlign":"center"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":225,"top":385.5,"height":38,"width":346.5,"title":"配置模块:主要为客户使用,开发人员可以配置属性,字段,标题等,客户直接使用,配置模块请参考实例2","fontFamily":"微软雅黑","lineHeight":15,"textAlign":"center","color":"#d93838"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":60,"top":487.5,"height":13,"width":123,"title":"长文本会自动分页","textAlign":"center"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":60,"top":507,"height":40,"width":511.5,"field":"longText"},"printElementType":{"title":"长文","type":"longText"}},{"options":{"left":475.5,"top":565.5,"height":100,"width":100},"printElementType":{"title":"矩形","type":"rect"}},{"options":{"left":174,"top":568.5,"height":13,"width":90,"title":"竖线","textAlign":"center"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":60,"top":574.5,"height":100,"width":10},"printElementType":{"title":"竖线","type":"vline"}},{"options":{"left":210,"top":604.5,"height":13,"width":120,"title":"横线","textAlign":"center"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":130.5,"top":625.5,"height":10,"width":277},"printElementType":{"title":"横线","type":"hline"}},{"options":{"left":364.5,"top":649.5,"height":13,"width":101,"title":"矩形","textAlign":"center"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":525,"top":784.5,"height":13,"width":63,"title":"页尾线","textAlign":"center"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":12,"top":786,"height":49,"width":49},"printElementType":{"title":"html","type":"html"}},{"options":{"left":75,"top":790.5,"height":13,"width":137,"title":"红色原型是自动定义的Html","textAlign":"center"},"printElementType":{"title":"自定义文本","type":"text"}},{"options":{"left":334.5,"top":810,"height":13,"width":205,"title":"页眉线已上。页尾下以下每页都会重复打印","textAlign":"center"},"printElementType":{"title":"自定义文本","type":"text"}}],"paperNumberLeft":565.5,"paperNumberTop":819}]}';
  282. return Json::decode($str);
  283. }
  284. }
粤ICP备19079148号