New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not able to get contents of divs with "display:none" #819
Comments
Html2canvas interprets the display:none style correctly and thus will not render a hidden element. To accomplish what you want, you can absolutely position a visible element off of the page viewport (if the desire is to "render a hidden element"). |
May be useful. |
It works if and only if the div has css which is not "display:none". so i used .show() and .hide() combination to make the element visible and hidden to form Canvas |
I found some info on Internet. We can set css to specified div with "display:none" then add option onclone like above. Plz add option "loggin: true" to see what happen |
Actually the trick of putting container out of a viewport with position absolute and top/left negative values - does not work in Firefox, it throws NS_ERROR_FAILURE exception It works though in Chrome |
I ran into this issue very recently, and if you want a particular div to be invisible and screenshotted, set the css properties as
|
This doesn't work for me @kchen1025 |
I ended up generating the canvas immediately after the target div, and then hiding the div immediately after the canvas was loaded. The flicker is barely noticeable. My code looks like this: <div id="capture">
...
</div>
<div id="end"></div> html2canvas(document.querySelector("#capture")).then(canvas => {
$("#end").append(canvas)
$("#capture").hide()
}); |
Hello, I'm working with Angular 6 and "html2canvas" to create a PDF with "jsPdf" .... I wanted to take a screenshot to a hidden div and I could not do it until I ACHIEVE it the capture is made to the div with id "html-pdf" "HTML" div id = "pdf" *ngIf = "test" style = "text-align: center; margin-top: 5px;"> /div "js or in angular is Typescript" html2canvas (document.getElementById ('html-pdf'), {scale:2}). Then (function (canvas) { var img = canvas.toDataURL ("image / png", 1); var doc = new jsPDF ({ doc.addImage (img, 'PNG', 2,2,212,272); And the most important thing to hide the div inside the screen. "CSS" the code creates a pdf with the capture of the div "html-pdf" that is inside the hidden div "pdf" ... I hope you serve them I wrote this in Spanish and it is translated, from Chile; |
I think the right way to do the job is to use |
clip-path will work .clipped {
clip-path: inset(0 100% 0 0);
} |
I used
|
I had the same problem. This is the best solution I found: eKoopmans/html2pdf.js#333 |
needed to hide the document(s) from UI (display: none) without show-hiding / no flicker etc. getSlottedReport(slot) {
const result = document.createElement('div');// container
// this will be different depending on how far down you need to get assignedElements from (2 web components down here)
const els = slot
.assignedElements()[0]
.shadowRoot.querySelectorAll('slot')[1]
.assignedElements();
// clone, allow to show and append
const addHtml = (el, div) => {
const r = el.cloneNode(true)
r.style.display = 'block'; // original is 'none' to hide
div.appendChild(r)
}
// inside any html template, the part(s) i want added to the PDF is enclosed in a div with a class named printable (which are display none / invisible to the UI)
els.map((el) => {
const ls = el.shadowRoot?.querySelectorAll('.printable')
// there is either a NodeList (ls) else a single Node (el)
if (ls) {
Array.from(ls).map(_el => addHtml(_el, result));
} else {
addHtml(el, result)
}
return undefined;
});
return result; // container with any wanted html appended
} example of using function above to generate blob: /**
* output the pdf as a blob
* @returns {Promise}
*/
@api
blob() {
const slot = this.template.querySelector('slot'); // main slot
const html = this.getSlottedReport(slot);
const options = this.options; // html2pdf options object
return html2pdf().from(html).set(options).outputPdf('blob');
} |
This worked for me ! Thanks @alphardex ! |
Set display: none in styles and then add this code, it worked for me.
onclone: function (clonedDoc) {
} |
Wow, good job, it works for me !!!!! thx |
you are a hero , much love and respect |
Hi I want to take screen shot of a div which is hidden
this is the code I tried
HTML:
JS:
Error in Browser:
**
**
My fiddle:https://jsfiddle.net/h39tp0Ly/2/
Please help me out with this
The text was updated successfully, but these errors were encountered: