vue.js不能使用window.print()在新页面中显示PDF
在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论