test.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <div id="test">
  3. <el-row :gutter="12">
  4. <el-col :offset="8" :span="8">
  5. <el-card shadow="always">
  6. <div slot="header" class="clearfix">
  7. <span>简单实例</span>
  8. </div>
  9. <div>
  10. <el-form label-width="80px">
  11. <el-form-item label="名字">
  12. <el-input v-model="from.name"></el-input>
  13. </el-form-item>
  14. <el-form-item label="条形码">
  15. <el-input v-model="from.code"></el-input>
  16. </el-form-item>
  17. <el-form-item label="页数">
  18. <el-input-number v-model="from.num" controls-position="right" :min="1" :max="10"> </el-input-number>
  19. </el-form-item>
  20. <el-form-item>
  21. <el-button type="primary" @click="print">生成打印文件</el-button>
  22. <el-button v-if="url" type="success" @click="openPdf">
  23. 打开生成文件
  24. </el-button>
  25. </el-form-item>
  26. </el-form>
  27. </div>
  28. </el-card>
  29. </el-col>
  30. </el-row>
  31. </div>
  32. </template>
  33. <script>
  34. import _ from 'lodash';
  35. import printTemplate from 'print-template';
  36. import { mapState, createNamespacedHelpers } from 'vuex';
  37. export default {
  38. name: 'test',
  39. props: {
  40. list: { type: Array, default: () => [] },
  41. },
  42. components: {},
  43. data: function() {
  44. return {
  45. url: '',
  46. template: null,
  47. from: { num: 1, name: '张三', code: 'YT121212323' },
  48. zhengshu: require('@/assets/zhengshu.jpg'),
  49. };
  50. },
  51. created() {
  52. this.init();
  53. },
  54. methods: {
  55. init() {
  56. this.template = new printTemplate();
  57. this.template.push({
  58. name: 'expressDelivery1',
  59. unit: 'mm',
  60. size: [210, 297],
  61. fixed: [{ type: 'image', x: 0, y: 0, default: this.zhengshu, width: 210, height: 297 }],
  62. data: {
  63. school_name: { type: 'text', x: 15, y: 15, fontSize: 3.5 },
  64. entry_year: { type: 'text', x: 56, y: 55, fontSize: 3.5 },
  65. major: { type: 'text', x: 107, y: 105, fontSize: 3.5 },
  66. name: { type: 'text', x: 158, y: 155, fontSize: 3.5 },
  67. termname: { type: 'text', x: 15, y: 255, fontSize: 3.5 },
  68. cernum: { type: 'text', x: 100, y: 250, fontSize: 3.5 },
  69. },
  70. });
  71. },
  72. openPdf() {
  73. let link = document.createElement('a');
  74. link.href = this.url;
  75. link.target = '_blank';
  76. link.click();
  77. },
  78. print() {
  79. let data = [];
  80. this.url = null;
  81. let duplicate = _.cloneDeep(this.list);
  82. duplicate = duplicate.map(i => {
  83. i.cernum = `2020${i.termname}${i.classname}`;
  84. return i;
  85. });
  86. this.template.print('expressDelivery1', [duplicate[0]]).then(pdf => {
  87. if (pdf) {
  88. this.$message.success('生成成功');
  89. this.url = pdf.output('bloburi', { filename: '证书' });
  90. } else {
  91. this.$message.warring('生成失败');
  92. }
  93. });
  94. },
  95. },
  96. computed: {
  97. ...mapState(['user']),
  98. pageTitle() {
  99. return `${this.$route.meta.title}`;
  100. },
  101. },
  102. metaInfo() {
  103. return { title: this.$route.meta.title };
  104. },
  105. };
  106. </script>
  107. <style lang="less" scoped></style>