App.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <div id="app">
  3. <div id="loading"></div>
  4. <h3 style="text-align: left;margin-left: 30px;">支持的打印方式:</h3>
  5. <el-divider><i class="el-icon-printer"></i></el-divider>
  6. <el-row :gutter="20">
  7. <el-col :span="8">
  8. <el-button type="primary"
  9. icon="el-icon-printer"
  10. v-print>全局打印</el-button>
  11. <el-card class="box-card">
  12. 对当前页面全部进行打印
  13. </el-card>
  14. </el-col>
  15. </el-row>
  16. <el-divider><i class="el-icon-printer"></i></el-divider>
  17. <el-row :gutter="20">
  18. <el-col :span="8">
  19. <el-button type="primary"
  20. icon="el-icon-magic-stick"
  21. v-print="'#printMe'">局部打印(快速)</el-button>
  22. <el-card class="box-card">
  23. 可以打印页面某部分内容,直接传入对应的唯一标识ID
  24. </el-card>
  25. </el-col>
  26. <el-col :span="8">
  27. <el-button type="primary"
  28. icon="el-icon-umbrella"
  29. v-print="printObj">局部打印(对象配置)</el-button>
  30. <el-card class="box-card">
  31. 也许你会需要一些配置,可接收一个对象,根据文档API进行设置
  32. </el-card>
  33. </el-col>
  34. </el-row>
  35. <el-divider><i class="el-icon-printer"></i></el-divider>
  36. <el-row :gutter="20">
  37. <el-col :span="8">
  38. <el-button type="primary"
  39. icon="el-icon-bank-card"
  40. v-print="printUrl">预览网址</el-button>
  41. <el-card class="box-card">
  42. 我需要打印指定的网址(要符合同源策略) 需要传入一个对象,详细看文档API进行设置
  43. </el-card>
  44. </el-col>
  45. <el-col :span="8">
  46. <el-button type="primary"
  47. icon="el-icon-wallet"
  48. v-print="printAsyncUrl">预览网址(异步)</el-button>
  49. <el-card class="box-card">
  50. 你打印的网址(URL)也是可以通过异步得到的
  51. </el-card>
  52. 异步获取URL:<el-progress :text-inside="true" :stroke-width="20" :percentage="times"></el-progress>
  53. </el-col>
  54. </el-row>
  55. <div>
  56. <el-divider><i class="el-icon-printer"></i></el-divider>
  57. <div class="asyncTips">
  58. </div>
  59. <div class="box"
  60. v-show="printLoading">
  61. <div class="loader-04"></div>
  62. 正在处理...请稍等
  63. </div>
  64. <h3 style="text-align: left;margin-left: 30px;">下面是被打印的例子:</h3>
  65. <div id="printMe"
  66. style="background: #dac9c9">
  67. <div ref="qrcode"></div>
  68. <div ref="echartMain"
  69. :style="{width: '300px', height: '300px'}"></div>
  70. <input type="number" />
  71. <input type="time" />
  72. <input type="checkbox" />
  73. <input type="radio" />
  74. <select>
  75. <option value="volvo">Volvo</option>
  76. <option value="saab">Saab</option>
  77. <option value="mercedes">Mercedes</option>
  78. <option value="audi">Audi</option>
  79. </select>
  80. <select>
  81. <option value="volvo">Volvo</option>
  82. <option value="saab">Saab</option>
  83. <option value="mercedes">Mercedes</option>
  84. <option value="audi">Audi</option>
  85. </select>
  86. <textarea name=""
  87. id=""
  88. cols="30"
  89. rows="10"></textarea>
  90. <p>葫芦娃,葫芦娃</p>
  91. <p style="background: yellow">一根藤上七朵花</p>
  92. <p>小小树藤是我家 啦啦啦啦</p>
  93. <p>叮当当咚咚当当 浇不大</p>
  94. <p>叮当当咚咚当当 是我家</p>
  95. <p>啦啦啦啦</p>
  96. <p>...</p>
  97. </div>
  98. <p>叮当当咚咚当当 浇不大</p>
  99. </div>
  100. </div>
  101. </template>
  102. <style type="text/css">
  103. p {
  104. color: blue;
  105. }
  106. </style>
  107. <script>
  108. import QRCode from "qrcodejs2";
  109. import * as echarts from "echarts";
  110. export default {
  111. name: "app",
  112. data () {
  113. return {
  114. printLoading: false,
  115. times: 0,
  116. printAsyncUrl: {
  117. preview: true,
  118. previewTitle: 'Test Title',
  119. previewPrintBtnLabel: 'Print',
  120. asyncUrl (reslove, vue) {
  121. const _set = setInterval(() => {
  122. vue.times += 20
  123. }, 1000)
  124. setTimeout(() => {
  125. reslove(process.env.NODE_ENV === 'production' ? 'https://power-kxlee.github.io/' : 'http://localhost:8080/')
  126. clearInterval(_set)
  127. }, 5000)
  128. },
  129. previewBeforeOpenCallback: this.previewBeforeOpenCallback ,
  130. previewOpenCallback:this.previewOpenCallback,
  131. beforeOpenCallback: this.beforeOpenCallback,
  132. openCallback: this.openCallback,
  133. closeCallback: this.closeCallback,
  134. clickMounted: this.clickMounted
  135. },
  136. printUrl: {
  137. url: process.env.NODE_ENV === 'production' ? 'https://power-kxlee.github.io/' : 'http://localhost:8080/',
  138. preview: true,
  139. previewTitle: 'Test Title',
  140. previewPrintBtnLabel: 'Print',
  141. previewBeforeOpenCallback: this.previewBeforeOpenCallback ,
  142. previewOpenCallback:this.previewOpenCallback,
  143. beforeOpenCallback: this.beforeOpenCallback,
  144. openCallback: this.openCallback,
  145. closeCallback: this.closeCallback,
  146. clickMounted: this.clickMounted
  147. },
  148. printObj: {
  149. id: "printMe",
  150. popTitle: "good print",
  151. extraCss: "https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.compat.css, https://cdn.bootcdn.net/ajax/libs/hover.css/2.3.1/css/hover-min.css",
  152. extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
  153. beforeOpenCallback: this.beforeOpenCallback,
  154. openCallback: this.openCallback,
  155. closeCallback: this.closeCallback,
  156. clickMounted: this.clickMounted
  157. },
  158. };
  159. },
  160. mounted () {
  161. this.$nextTick(() => {
  162. this.echart();
  163. new QRCode(this.$refs.qrcode, {
  164. width: 100,
  165. height: 100,
  166. text: "https://www.baidu.com/",
  167. });
  168. });
  169. },
  170. methods: {
  171. clickMounted(vue) {
  172. vue.$notify({
  173. title: '消息',
  174. message: '点击按钮的回调事件',
  175. });
  176. },
  177. previewBeforeOpenCallback(vue) {
  178. vue.$notify({
  179. title: '消息',
  180. message: '正在加载预览窗口',
  181. });
  182. },
  183. previewOpenCallback(vue) {
  184. vue.times = 0
  185. vue.$notify({
  186. title: '消息',
  187. message: '已经加载完预览窗口',
  188. type: 'success'
  189. });
  190. },
  191. beforeOpenCallback(vue) {
  192. vue.printLoading = true
  193. vue.$notify({
  194. title: '消息',
  195. message: '正在准备打印控件',
  196. });
  197. },
  198. openCallback(vue) {
  199. vue.printLoading = false
  200. vue.$notify({
  201. title: '消息',
  202. message: '已经打开了 打印控件',
  203. });
  204. },
  205. closeCallback(vue) {
  206. vue.$notify({
  207. title: '消息',
  208. message: '关闭了打印工具',
  209. });
  210. },
  211. beforeOpen () {
  212. this.printLoading = true
  213. console.log('准备打开')
  214. },
  215. echart () {
  216. let myChart = echarts.init(this.$refs.echartMain);
  217. myChart.setOption({
  218. title: {
  219. text: "ECharts 入门示例",
  220. },
  221. tooltip: {},
  222. xAxis: {
  223. data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
  224. },
  225. yAxis: {},
  226. series: [
  227. {
  228. name: "销量",
  229. type: "bar",
  230. data: [5, 20, 36, 10, 10, 20],
  231. },
  232. ],
  233. });
  234. },
  235. },
  236. };
  237. </script>
  238. <style scoped lang="scss">
  239. #app::v-deep {
  240. font-family: "Avenir", Helvetica, Arial, sans-serif;
  241. -webkit-font-smoothing: antialiased;
  242. -moz-osx-font-smoothing: grayscale;
  243. text-align: center;
  244. color: #2c3e50;
  245. margin-top: 60px;
  246. width: 800px;
  247. margin: 0 auto;
  248. .el-row {
  249. margin-bottom: 20px;
  250. &:last-child {
  251. margin-bottom: 0;
  252. }
  253. .box-card {
  254. margin-top: 10px;
  255. }
  256. }
  257. }
  258. [class*="loader-"] {
  259. display: inline-block;
  260. width: 1em;
  261. height: 1em;
  262. color: inherit;
  263. vertical-align: middle;
  264. pointer-events: none;
  265. }
  266. .loader-04 {
  267. border: 1px solid currentcolor;
  268. border-radius: 50%;
  269. -webkit-animation: 1s loader-04 linear infinite;
  270. animation: 1s loader-04 linear infinite;
  271. position: relative;
  272. }
  273. .loader-04:before {
  274. content: "";
  275. display: block;
  276. width: 0;
  277. height: 0;
  278. position: absolute;
  279. top: -0.2em;
  280. left: 50%;
  281. border: 0.2em solid currentcolor;
  282. border-radius: 50%;
  283. }
  284. @-webkit-keyframes loader-04 {
  285. 0% {
  286. -webkit-transform: rotate(0deg);
  287. transform: rotate(0deg);
  288. }
  289. 100% {
  290. -webkit-transform: rotate(360deg);
  291. transform: rotate(360deg);
  292. }
  293. }
  294. @keyframes loader-04 {
  295. 0% {
  296. -webkit-transform: rotate(0deg);
  297. transform: rotate(0deg);
  298. }
  299. 100% {
  300. -webkit-transform: rotate(360deg);
  301. transform: rotate(360deg);
  302. }
  303. }
  304. </style>