top of page
Writer's pictureSaurabh Singh

Create screenshot from DIV in LWC

There are some ways to take screenshot from div in javascript but in LWC, we need to modify to make it LWC compatible.


First write html as follows



in JS you need to import html2canvas.js. you can download this js from this location.




using printDiv function we can create screenshot and display on the page.

Remember we can display image on Canvas as well, but in my case I was getting blurry images on canvas, so instead on canvas i displayed it on img tag.

5,083 views10 comments

10 Comments


Sridhar NareshIT
Sridhar NareshIT
Dec 15, 2023

Thanks for Making my job very easier , very helpful blog Keep sharing more blogs on salesforce

Like

Nevo Harari
Nevo Harari
Jul 03, 2023

and Can't see inside the js lib


Like

Nevo Harari
Nevo Harari
Jul 03, 2023

I also get caught (in promise) Unable to find element in cloned iframe

Like

Souvik Khan
Souvik Khan
May 04, 2023

My requirement is kinda same, want to take screenshot/convert lwc component to img/svg format. Using html2canvas as above, it showing this error: caught (in promise) Unable to find element in cloned iframe Any idea?

Like
Tommy
Tommy
Sep 07, 2023
Replying to

Also, doesn't work in Chrome for me either. would love if the Author could explain their solution!

Like

Tom Tam
Tom Tam
Jan 11, 2023

Hi do you have screen shots of this HTML2CANVAS in action on the LWC? When I copy your example I get an error: [0 .default] is not a function


Do you have any recommendations on how to overcome this?

Like
Tommy
Tommy
Sep 07, 2023
Replying to

your static resource reference is the same name as the method, change import HTML2CANVAS from '@salesforce/resourceUrl/HTML2CANVAS';


to


import HTML2CANVASref from '@salesforce/resourceUrl/HTML2CANVAS';
and then
loadScript(this, HTML2CANVASref).then(() => {
		console.log('html2canvas loaded');
	});
Like
bottom of page