0


vue项目纯前端实现的“模板打印“功能

下载一个插件 npm i vue-print-nb --save

在main中引入 import Print from “vue-print-nb”

  1. Vue.use(Print);

在postcss.config.js里面展示这个数据样式,如果你的项目中没有这个文件

那就下载一个插件"npm i postcss-px-to-view --save-dev";

  1. module.exports = {
  2. plugins: {
  3. autoprefixer: {},
  4. "postcss-px-to-viewport": {
  5. viewportWidth: 1920, // 视窗的宽度,对应的是我们设计稿的宽度,移动端一般是750,如果是pc端那就是类似1920这样的尺寸
  6. // viewportHeight: 1344, // 视窗的高度,移动端一般指定1334,也可以不配置
  7. unitPrecision: 3, // 指定`px`转换为视窗单位值的小数位数(很多时候无法整除)
  8. viewportUnit: "vw", // 指定需要转换成的视窗单位,建议使用vw
  9. selectorBlackList: ['.nocalssvw'], // 指定不转换为视窗单位的类,可以自定义,可以无限添加,建议定义一至两个通用的类名
  10. exclude: [/printPersone/],
  11. // propList:["*", "!font-size"],//能转化为vw的属性列表
  12. minPixelValue: 1, // 小于或等于`1px`不转换为视窗单位,你也可以设置为你想要的值
  13. mediaQuery: false // 允许在媒体查询中转换`px`
  14. }
  15. }
  16. };

创建一个和“selectorBlackList”里面名字一样的vue,如上:printPersone.vue

  1. 父组件
  2. <template>
  3. <div>
  4. <el-dialog title="表" :visible.sync="dialogVisible" width="70%" :before-close="handleClose">
  5. <el-button type="primary" plain style="margin-bottom:5px;" @click="handlePrint">打印</el-button>
  6. <el-row>
  7. <el-col :span="12">
  8. <div>
  9. <table border="1" class="tableOne" v-for="(item, index) in dataList" :key="index">
  10. <thead>
  11. <tr>
  12. <th>姓名</th>
  13. <td>张三</td>
  14. <th>性别</th>
  15. <td>男</td>
  16. <th>出生年月</th>
  17. <td>1985.5.10</td>
  18. <td rowspan="4" style="width: 130px;"></td>
  19. </tr>
  20. <tr>
  21. <th>民族</th>
  22. <td>汉</td>
  23. <th>籍贯</th>
  24. <td>汉</td>
  25. <th>出生地</th>
  26. <td>山东</td>
  27. </tr>
  28. </thead>
  29. </table>
  30. </div>
  31. </el-col>
  32. </el-row>
  33. <!-- 引用那个打印的模板 -->
  34. <print-person ref="myPrintPerson" :list="dataList" />
  35. </el-dialog>
  36. </div>
  37. </template>
  38. <script>
  39. import PrintPerson from './components/printPersone.vue'
  40. export default {
  41. components: {
  42. PrintPerson,
  43. },
  44. data() {
  45. return {
  46. dialogVisible: false,
  47. dataList: [],
  48. };
  49. },
  50. created() {
  51. },
  52. methods: {
  53. init(dataList) {
  54. this.dialogVisible = true;
  55. this.dataList = dataList;
  56. this.getList();
  57. },
  58. handleClose(done) {
  59. done();
  60. },
  61. // 打印按钮的事件
  62. handlePrint() {
  63. let refPrint = this.$refs['myPrintPerson']
  64. refPrint && refPrint.print()
  65. },
  66. }
  67. };
  68. </script>

打印的模板

  1. 打印的模板组件
  2. <template>
  3. <div>
  4. <button ref="printbtn" class="myprintbtn" v-print="'#myprintDom'"></button>
  5. <div id="myprintDom" class="nocalssvw">
  6. <div class="print-warp" style="page-break-before:always;" v-for="(item, ix) in list" :key="ix">
  7. <table border="1" class="primt-table print-tableOne">
  8. <thead>
  9. <tr>
  10. <td style="width: 68px;" class="pt">姓名</td>
  11. <td style="width: 58px;">张三</td>
  12. <td style="width: 68px;" class="pt">性别</td>
  13. <td style="width: 68px;" class="ptw84">男</td>
  14. <td style="width: 68px;" class="pt">出生年月</td>
  15. <td style="width: 68px;">1987.5.10</td>
  16. <td rowspan="4" colspan="2" style="width: 120px;"></td>
  17. </tr>
  18. <tr>
  19. <td class="pt">民族</td>
  20. <td>汉</td>
  21. <td class="pt">籍贯</td>
  22. <td>汉</td>
  23. <td class="pt">出生地</td>
  24. <td>山东</td>
  25. </tr>
  26. </thead>
  27. </table>
  28. </div>
  29. </div>
  30. </div>
  31. </template>
  32. <script>
  33. export default {
  34. props: {
  35. list: {
  36. type: Array,
  37. default: () => [],
  38. required: true,
  39. },
  40. },
  41. data() {
  42. return {
  43. myPrint: {
  44. id: 'myprintDom',
  45. extarCss: ''
  46. }
  47. }
  48. },
  49. methods: {
  50. print() {
  51. this.$refs['printbtn'].click();
  52. }
  53. },
  54. }
  55. </script>

本文转载自: https://blog.csdn.net/m0_66226018/article/details/133747817
版权归原作者 _chang. 所有, 如有侵权,请联系我们删除。

“vue项目纯前端实现的“模板打印“功能”的评论:

还没有评论