如何在不丢失样式的情况下打印 vue 组件的一部分

Tan*_*may 12 javascript printing vue.js

我想从 vue 组件打印一些内容。例如,从以下代码段中,我想打印v-card-textid 为的元素#print

new Vue({
  el: '#app',
  data() {
    return {
      dialog: false
    }
  },
  methods: {
    print() {
      var prtContent = document.getElementById("print");
      var WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
      WinPrint.document.write(prtContent.innerHTML);
      WinPrint.document.close();
      WinPrint.focus();
      WinPrint.print();
      WinPrint.close();
    }
  }
})
Run Code Online (Sandbox Code Playgroud)
<!doctype html>
<html>

<head>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/vuetify/1.2.0/vuetify.min.css" />
</head>

<body>
  <div id="app">
    <v-app id="inspire">
      <v-layout row justify-center>
        <v-dialog v-model="dialog" persistent max-width="290">
          <v-btn slot="activator" color="primary" dark>Open Dialog</v-btn>
          <v-card>
            <v-card-title class="headline">Print This:</v-card-title>
            <v-card-text id="print">Lorem ipsum dolor sit amet.</v-card-text>
            <v-card-actions>
              <v-spacer></v-spacer>
              <v-btn color="green darken-1" flat @click.native="print">Print</v-btn>
          </v-card>
        </v-dialog>
      </v-layout>
    </v-app>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/vuetify/1.2.0/vuetify.min.js"></script>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

但是,当我收到打印提示时,与之相关的所有样式都消失了。如何以这种方式打印 vue 组件,以便组件不会丢失关联的样式?我建议将代码片段复制到您的本地机器以获得最佳效果。

Dec*_*oon 18

您需要从原始文档中复制样式。尝试这样的事情:

// Get HTML to print from element
const prtHtml = document.getElementById('print').innerHTML;

// Get all stylesheets HTML
let stylesHtml = '';
for (const node of [...document.querySelectorAll('link[rel="stylesheet"], style')]) {
  stylesHtml += node.outerHTML;
}

// Open the print window
const WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');

WinPrint.document.write(`<!DOCTYPE html>
<html>
  <head>
    ${stylesHtml}
  </head>
  <body>
    ${prtHtml}
  </body>
</html>`);

WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
Run Code Online (Sandbox Code Playgroud)

  • 这节省了我几个小时!您是否也知道如何加载图标?我正在使用 Google Material Icons,样式表的链接显示在 styleHtml 中,但我只是收到文本。我什至尝试将 `&lt;link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons"/&gt;` 显式添加到 `document.write 的 `&lt;head&gt;` 部分()` 没有运气。有任何想法吗? (4认同)
  • 如果您无法打印图像,这是因为调用打印命令时,窗口尚未加载图像。使用 ```js window.setTimeout(() =&gt; {WinPrint.focus(); WinPrint.print()}, 200)``` (3认同)

小智 5

安装这个包vue-html-to-paper

npm install vue-html-to-paper
Run Code Online (Sandbox Code Playgroud)

用法:

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);
Run Code Online (Sandbox Code Playgroud)

成分:

<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>
Run Code Online (Sandbox Code Playgroud)

更多细节检查:https : //randomcodetips.com/vue-html-to-paper/