Vue 输出 HTML 打印

本文介绍了如何使用Vue.js插件vue-html-to-paper将HTML元素打印到纸张上。该插件提供了一种简单的方法来实现网页内容的打印功能,并且支持自定义打印设置和样式。通过引入和配置,可以在Vue组件中轻松调用打印方法。此外,还提到了如何设置横向打印以及使用回调函数处理打印完成后的操作。
摘要由CSDN通过智能技术生成

Vue 输出 HTML 打印

在这里插入图片描述

Vue HTML to Paper

Vue mixin用于纸张打印html元素。

演示版

https://mycurelabs.github.io/vue-html-to-paper/

安装

NPM

npm install vue-html-to-paper

yarn

yarn add vue-html-to-paper

CDN

https://unpkg.com/vue-html-to-paper/build/vue-html-to-paper.js

用法

main.js

import Vue from 'vue';
import VueHtmlToPaper from 'vue-html-to-paper';

const options = {
  name: '_blank',
  specs: [
    'fullscreen=yes',
    'titlebar=yes',
    'scrollbars=yes'
  ],
  styles: [
    'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css',
    'https://unpkg.com/kidlat-css/css/kidlat.css'
  ]
}

Vue.use(VueHtmlToPaper, options);

// or, using the defaults with no stylesheet
Vue.use(VueHtmlToPaper);

请参阅此处的window.openAPI 。

组件形式

<template>
  <div>
    <!-- SOURCE -->
    <div id="printMe">
      <h1>Print me!</h1>
    </div>
    <!-- OUTPUT -->
    <button @click="print"></button>
  </div>
<template>

<script>
export default {
  data () {
    return {
      output: null
    }
  },
  methods: {
    print () {
      // Pass the element id here
      this.$htmlToPaper('printMe');
    }
  }
}
</script>

带有本地选项

options = {
  name: '_blank',
  specs: [
    'fullscreen=yes',
    'titlebar=yes',
    'scrollbars=yes'
  ],
  styles: [
    'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css',
    'https://unpkg.com/kidlat-css/css/kidlat.css'
  ]
}

返回值

打印完成后(无论是否成功),请使用回调函数来通知。不需要回调方法。

笔记

使用回调时,请注意,本地选项现在有第二个参数。因此,回调将是第三个参数。null如果您不想覆盖全局选项,则作为第二个参数传递。

this.$htmlToPaper('printMe', null, () => {
  console.log('Printing completed or was cancelled!');
});

常问问题

如何横向打印

在全局选项中,可以通过以下方式传递css:

@media print {
  @page {
    size: landscape
  }
}

然后,将自定义css注入样式选项中,如下所示:

const options = {
  styles: [
    'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css',
    './path/to/custom.css' // <- inject here
  ]
}

这也可以通过使用local选项来完成。

Made with ❤️ by Jofferson Ramirez Tiquez 翻译 sweetwisdom

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值