Screenshot and Download in HTML | HTML2Canvas and Canvas2Image
About HTML2Canvas
HTML2Canvas allows you to create a "screenshot" of whole webpage or a particular part of it. The screenshot is saved as canvas directly on the user's browser. The canvas is capture exactly based on the data available on the page, but The canvas which is captured may differ and quality is poor as compared to its presentation on the user's browser.
About Canvas2Image
A tool of saving or converting canvas to images
Creating a file that takes screenshots and download/saves it locally
In this post I will be sharing how I used HTML2Canvas and Canvas2Image to create a screenshot and then download of it locally. I have been using HTML2Canvas recently and wanted to download the screenshot as well. Well finally I got the solution from here and there, specially from StackOverflow.
I wanted to have a page having multiple div(s) having different contents and dedicated download buttons for each. But the method that I found was having seperate defined functions for each download button. That was such a hassle. I mean who would like to have seperate function for same task just because you have to select different element. So, I used JQuery to select each div by ID.
To make this work I used previous version of HTML2Canvas because I wanted to use onrendered feature which is obsolute in the latest version.
HTML2Canvas.js 0.4.1 DownloadHTML Code
By Bishal Biswas
10-Mar-21, 9:25 PM
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Earum illum consequuntur nihil dicta cupiditate suscipit ducimus fugit hic ratione.
I have given each div an ID and added an attribute 'data-id' having the value of the ID of the div. Later using JQuery I am accessing the ID using this attribute and putting it in the html2canvas function
CSS Code
JavaScript & JQuery Code
As I have set each and every download button a same class 'btnSave', I am using 'this' keyword to access the particular element which was clicked. Then using '$(this).attr("data-id")' to get the ID and then passsing it to the html2canvas function
Screenshots:
Working Solution:
See the Pen HTML2Canvas Screenshot and Download image by bishal-biswas (@bishal-biswas) on CodePen.
Conclusion
I am trying to find solution with the latest version of HTML2Canvas as we can get high quality of the canvas while capture. I will be uploading the solution as soon as I get it working.
If you like this article or find it helpful, please do share it with colleagues and friends.
Comments