vue.js不能使用window.print()在新页面中显示PDF

发布于 2025-02-13 18:54:46 字数 1940 浏览 0 评论 0原文

在page.vue中,PDF读取器用于将PDF嵌入HTML中,并且使用window.print()

<template>

    <button class="btn ml-1"@click="handleWindowPrint()">Export PDF</button>
....
    <div class="pdf-export" v-for="record in pdfRecords" :key="record.key">
        <PdfReader :documentUrl="record" @onclick-remove-pdf-btn="removePdfRecord" />
    </div
....
</template>

handleWindowPrint() {
    const printPage = window.open('', '_blank')
    const pdf = document.querySelector('.pdf-export')
    const view = {
        ...
    };
    const html = Mustache.render(`${this.templateUsed}`, view);
    if (printPage) {
        printPage.document.head.innerHTML =
            '<meta charset="utf-8">\n' +
            ' <meta name="format-detection" content="telephone=no">\n' +
            ' <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\n' +
            ' <meta name="viewport" content="width=device-width,initial-scale=1.0">\n'
        
        printPage.document.body.innerHTML = html + pdf?.outerHTML
        setTimeout(() => {
            printPage.print();
        }, 500);
    }
}

pdfreader是使用vue-pdf:何时使用vue-pdf:

<template>
  <div>
    <label><small>(Perview Three Page)</small></label>
    <figure class="image-item pdf-item">  
      <a >
        <pdf :key="i" :src="url"  v-for="i in numPages" :page="i"></pdf>
      </a>
    </figure>
  </div>
</template>
<script>
import pdf from "vue-pdf";

export default {
  name: "PdfReader",
  props: ["documentUrl", "removeable"],
  components: {
    pdf,
  },
  data: function () {
    return {
      url: this.documentUrl,
      numPages: 3,
    };
  },
</style>

何时 在新页面中导出PDF的按钮:我单击按钮,新页面(打印页)不能pdfk它是黑色的,但是在页面上,pdf可以完美显示。 代码中有什么错吗?还是我想念什么?

任何建议都将不胜感激。

In page.vue, PDF Reader is used to embed pdf into html and there are a button to export pdf in the new page using window.print()

<template>

    <button class="btn ml-1"@click="handleWindowPrint()">Export PDF</button>
....
    <div class="pdf-export" v-for="record in pdfRecords" :key="record.key">
        <PdfReader :documentUrl="record" @onclick-remove-pdf-btn="removePdfRecord" />
    </div
....
</template>

handleWindowPrint() {
    const printPage = window.open('', '_blank')
    const pdf = document.querySelector('.pdf-export')
    const view = {
        ...
    };
    const html = Mustache.render(`${this.templateUsed}`, view);
    if (printPage) {
        printPage.document.head.innerHTML =
            '<meta charset="utf-8">\n' +
            ' <meta name="format-detection" content="telephone=no">\n' +
            ' <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\n' +
            ' <meta name="viewport" content="width=device-width,initial-scale=1.0">\n'
        
        printPage.document.body.innerHTML = html + pdf?.outerHTML
        setTimeout(() => {
            printPage.print();
        }, 500);
    }
}

PdfReader is a component that using vue-pdf:

<template>
  <div>
    <label><small>(Perview Three Page)</small></label>
    <figure class="image-item pdf-item">  
      <a >
        <pdf :key="i" :src="url"  v-for="i in numPages" :page="i"></pdf>
      </a>
    </figure>
  </div>
</template>
<script>
import pdf from "vue-pdf";

export default {
  name: "PdfReader",
  props: ["documentUrl", "removeable"],
  components: {
    pdf,
  },
  data: function () {
    return {
      url: this.documentUrl,
      numPages: 3,
    };
  },
</style>

When I click button, new page (printPage) cannot pdfk it is black, but in page.vue page, pdf can display perfectly.
Is anything wrong in the code? Or am I missing something?

Any advice would be appreciated Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文