我正在使用 html2pdf 生成 pdf,我可以隐藏 html 以便用户看不到它吗?
I'm using html2pdf to generate a pdf, can I hide the html so the user doesn't see it?
我正在使用 html2pdf 库生成代金券。
这适用于在页面中显示为 HTML 的凭证。
我有一个按钮可以在点击时触发 html2pdf()
功能,提示用户接受 PDF 下载。
我希望 HTML 不显示在页面上。我尝试应用 position: absolute;
并将 HTML 放在远离用户视线的地方。不幸的是,PDF 随后呈现为空白。
有办法实现吗?
只需在 html2pdf 调用前后切换显示 'element-to-print' 的 属性。
https://jsfiddle.net/bambang3128/u6o6ne41/10/
function toggleDisplay() {
var element = document.getElementById('element-to-print');
if (element.style.display == 'block') {
element.style.display = 'none'
} else {
element.style.display = 'block'
}
console.log('toggleDisplay()');
}
function printPDF() {
var element = document.getElementById('element-to-print');
element.style.display = 'block'
html2pdf(element);
element.style.display = 'none'
console.log('printPDF()');
}
<script src="https://rawgit.com/eKoopmans/html2pdf/master/dist/html2pdf.bundle.min.js"></script>
<div id="element-to-print" hidden>
<h1>This is a hidden div</h1>
This one is hidden div contents.
</div>
<p>
Save the hidden element as PDF.
</p>
<button type="button" onclick="toggleDisplay();">Toggle Display!</button>
<button type="button" onclick="printPDF();">Click Me!</button>
您可以使用绝对定位并将您的 div wayyyyyy 放在屏幕之外,例如 top: 10000000000。
然后在处理完图像/pdf 后将 div 带走!
您只需将 display:none;
分配给您的元素,如下所示:
<div id="element-to-print" style="display:none">
</div>
<button type="button" (click)="html2pdf()">Download as PDF</button>
只需使用 显示隐藏元素:none 属性。该元素不会出现在下载的 pdf 中。
举个例子:
function download() {
var element = document.getElementById("report");
// if you want to show the element in pdf
/*
for showing element in pdf
var hidden = document.querySelector('.hidden');
hidden.style.display = "block";
*/
html2pdf(element)
}
.hidden {
display: none;
}
<script src="https://rawgit.com/eKoopmans/html2pdf/master/dist/html2pdf.bundle.min.js"></script>
<button onclick="download()">Download PDF</button>
<div id="report">
<div class="hidden">Hidden Element</div>
<div>Content to be shown in PDF</div>
</div>
如果你想在不向用户显示内容的情况下下载 pdf,请使用 innerHTML
<div id="exportPdf" style="display: none"></div>
style="display: none"
到 div
var source = window.document.getElementById("exportPdf").innerHTML;
html2pdf().set(opt).from(source).save();
innerHTML 可以解决问题
不传递元素,只需将 HTML 作为字符串传递:
html2pdf().from("<h1>Title</h1>").save();
在我的例子中,我使用的是 ReactJS,所以我会这样做:
const htmlAsString = ReactDOMServer.renderToString(<Row>
<Col md="6">Section 1</Col>
<Col md="6">Section 2</Col>
</Row>);
html2pdf().from(htmlAsString).save();
在 VueJS 应用程序的上下文中,我遇到了类似的情况,我只想在生成 PDF 时显示内容。
因此您可以简要 'show' 内容并生成 PDF 并立即隐藏它。
<html2-pdf>
<section slot="pdf-content" :class="generatingPDF ? 'inline-block' : 'hidden">
// Content goes here
</section>
</html2-pdf>
方法中:
methods: {
async generatePDF(){
try {
this.generatedPDF = true
// generating PDF code here
} catch (e) {
console.log(e)
} finally {
this.generatingPDF = false
}
}
}
根据我的经验,用户不会'flashed'应该隐藏的pdf内容。
我正在使用 html2pdf 库生成代金券。
这适用于在页面中显示为 HTML 的凭证。
我有一个按钮可以在点击时触发 html2pdf()
功能,提示用户接受 PDF 下载。
我希望 HTML 不显示在页面上。我尝试应用 position: absolute;
并将 HTML 放在远离用户视线的地方。不幸的是,PDF 随后呈现为空白。
有办法实现吗?
只需在 html2pdf 调用前后切换显示 'element-to-print' 的 属性。
https://jsfiddle.net/bambang3128/u6o6ne41/10/
function toggleDisplay() {
var element = document.getElementById('element-to-print');
if (element.style.display == 'block') {
element.style.display = 'none'
} else {
element.style.display = 'block'
}
console.log('toggleDisplay()');
}
function printPDF() {
var element = document.getElementById('element-to-print');
element.style.display = 'block'
html2pdf(element);
element.style.display = 'none'
console.log('printPDF()');
}
<script src="https://rawgit.com/eKoopmans/html2pdf/master/dist/html2pdf.bundle.min.js"></script>
<div id="element-to-print" hidden>
<h1>This is a hidden div</h1>
This one is hidden div contents.
</div>
<p>
Save the hidden element as PDF.
</p>
<button type="button" onclick="toggleDisplay();">Toggle Display!</button>
<button type="button" onclick="printPDF();">Click Me!</button>
您可以使用绝对定位并将您的 div wayyyyyy 放在屏幕之外,例如 top: 10000000000。
然后在处理完图像/pdf 后将 div 带走!
您只需将 display:none;
分配给您的元素,如下所示:
<div id="element-to-print" style="display:none">
</div>
<button type="button" (click)="html2pdf()">Download as PDF</button>
只需使用 显示隐藏元素:none 属性。该元素不会出现在下载的 pdf 中。
举个例子:
function download() {
var element = document.getElementById("report");
// if you want to show the element in pdf
/*
for showing element in pdf
var hidden = document.querySelector('.hidden');
hidden.style.display = "block";
*/
html2pdf(element)
}
.hidden {
display: none;
}
<script src="https://rawgit.com/eKoopmans/html2pdf/master/dist/html2pdf.bundle.min.js"></script>
<button onclick="download()">Download PDF</button>
<div id="report">
<div class="hidden">Hidden Element</div>
<div>Content to be shown in PDF</div>
</div>
如果你想在不向用户显示内容的情况下下载 pdf,请使用 innerHTML
<div id="exportPdf" style="display: none"></div>
style="display: none"
到 div
var source = window.document.getElementById("exportPdf").innerHTML;
html2pdf().set(opt).from(source).save();
innerHTML 可以解决问题
不传递元素,只需将 HTML 作为字符串传递:
html2pdf().from("<h1>Title</h1>").save();
在我的例子中,我使用的是 ReactJS,所以我会这样做:
const htmlAsString = ReactDOMServer.renderToString(<Row>
<Col md="6">Section 1</Col>
<Col md="6">Section 2</Col>
</Row>);
html2pdf().from(htmlAsString).save();
在 VueJS 应用程序的上下文中,我遇到了类似的情况,我只想在生成 PDF 时显示内容。
因此您可以简要 'show' 内容并生成 PDF 并立即隐藏它。
<html2-pdf>
<section slot="pdf-content" :class="generatingPDF ? 'inline-block' : 'hidden">
// Content goes here
</section>
</html2-pdf>
方法中:
methods: {
async generatePDF(){
try {
this.generatedPDF = true
// generating PDF code here
} catch (e) {
console.log(e)
} finally {
this.generatingPDF = false
}
}
}
根据我的经验,用户不会'flashed'应该隐藏的pdf内容。