Open lesterjanpearson opened 6 years ago
@lesterjanpearson Do you mean to hide some parts of the PDF?
<body>
<section id="pdf">
<div>Hello World</div>
<div data-html2canvas-ignore>I will not be shown</div>
</section>
</body>
Hi @lesterjanpearson, it seems to me you either meant:
.from()
, you can either pass an element or an HTML string). That will create it entirely invisibly.Let us know if that helps!
what I am currently doing now is, hidding the content. then when user clicks download pdf button, show it until the callback and dowloads, then hide i back.
@eKoopmans I know this is a very old thread but since it's still open and I've just needed this functionality recently, I'm sharing a piece of code that works with the latest version of html2pdf, in case someone needs it:
<div class="your-target-div-here" style="display: none;">
This is the target DIV, to be "printed" inside the generated PDF file.
</div>
const opt = {
margin: 0,
filename: "any-name-here",
image: { type: "jpeg", quality: 1 },
html2canvas: {
scale: 5,
logging: true,
letterRendering: true,
useCORS: true,
onclone: function (doc) {
doc.querySelector(".your-target-div-here").style.display = "block";
},
},
jsPDF: {
unit: "in",
format: "A4",
orientation: "landscape",
},
};
html2pdf()
.set(opt)
.from(element)
.toPdf()
.save();
@mmoraes I did the same thing but I don't know why it is not working, would you please check it once.
HTML
<div class="qrCodeDiv" style="display: none!important;">
//html elements
</div>
TS
const element = document.getElementById('report-section');
const options = {
filename: 'report.pdf',
image: { type: 'jpeg', quality: 1 },
html2canvas: {
scale: 2,
logging: true,
letterRendering: true,
useCORS: true,
onclone: function (doc) {
doc.querySelector(".qrCodeDiv").style.display = "block";
},
},
jsPDF: { unit: 'mm', format: 'letter', orientation: 'portrait' },
pagebreak: { after: ['#cover', '.section', '.page-break'] }
}
var worker = html2pdf().set(options)
.from(element)
.toPdf()
.save();
@najlaahmadyar Try without !important
or this other approach with the <div>
tags:
<div style="display: none" class="qrCodeDiv">
<div style="top: -9999999px; position: absolute">Content to appear in the PDF file</div>
</div>
@mmoraes I think in my case onclone is not running. for testing, I changed the color of an element in that function but it didn't affect at all.
Is there any documentation on how to use these options?
@najlaahmadyar Which version are you running? Earlier versions don't have this feature, I think.
@mmoraes html2pdf.js version 0.9.2, which version I should use. seems the older versions don't support the page breaks.
@najlaahmadyar I'm not sure which version started being compatible with onclone
but I certainly advise you to use the latest version, as it fixes several bugs and adds new features.
@najlaahmadyar onclone func is working but doc.querySelector is not working. Is there any other solution for this? I'm using the latest version 0.9.3
The querySelector
works, but it seems the output of the onclone function is not used.
@TomDeSmet Did you solve this already? I am having the same problem
It's been a while but I believe I used innerHTML
to get the hidden contents I want to print as a HTML string and pass that to the from()
function.
Just faced this issue, solved it with this :
HTML - The hidden div that will be used in my pdf
<div id="element-to-convert" class="element-to-convert" style="display: none;">
<!-- Your pdf content -->
</div>
JS - The logic for downloading the pdf
// Choose the element that your pdf will render .
const element = document.getElementById("element-to-convert");
// Clone the element
var clonedElement = element.cloneNode(true);
// Change the display rule of the cloned element
clonedElement.style.display = "block"
// Generate pdf
html2pdf(clonedElement, options);
// Remove the cloned element
clonedElement.remove();
@ThibautCerba Your solution works perfectly! Thanks a lot!
@ThibautCerba solution works fine ,thanks a lot!
The solution works, but I'm using EChart in my app and it is not converted to pdf. I'm getting the empty canvas
Is there a way to print the pdf, with the real contents on html hidden?