SlideShare a Scribd company logo
Solution to Capture Webpage Screenshot with
Html2Canvas.js for Backend Developers
Recently, I was to implement a web application requirement that the screenshot of a web
page containing sketches done by users be captured, as the image is being saved. While
this may seem trivial, a lot of intricacies is involved in building an image representation of
the page as it is shown on the browser.
However, when I tried to implement that, it wasn’t so easy as I expected. Some old ways
offer ActiveX but it seems too outdated. Since there’s a bridge needed between client side
and server, JS libraries are the best way.There’s a great library, html2canvas. It is told to
be reverse-engineered version of Google Plus’ way of taking screenshots.
Html2canvas.js is a JavaScript library that provides functionality for capturing a
screenshot; the screenshot is built based on the information available on the page. The
script traverses the DOM and builds a representation of the page alongside the images
and the CSS styles present on the page, but it doesn’t render flash, java applets, and
iframe. The html2canvas library can be downloaded here.
When I first discovered this library, it took me a while to use for simplest implementation.
I just wanted to visualize a div element. However, there was no single page to tell the
whole path to follow, thus I had to combine various sources.
Here have how you can easily use for taking a screenshot of a div. There are three libraries
to import:
1. jquery.js
2. html2canvasjs
3. plugin.html2canvas.js
It uses the jQuery plugin HTML2Canvas that converts a given page section to a graphic
image on HTML5 canvas element and submits the captured image data to the server via
AJAX. The script traverses through the DOM of the page it is loaded on. It gathers
information on all the elements there, which it then uses to build a representation of the
page. In other words, it does not actually take a screenshot of the page but builds a
representation of it based on the properties it reads from the DOM.
I need to convert entire div to image and save to our server in my web application. So I do
provide some code below for your understanding purpose.
1. HTML code for Div which we have to capture as an image.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div id="mainDiv">
Welcome to eLuminous Technologies
<img src="logo.png" alt="Eluminous Technologies">
</div>
<!-----------add image url after ajax response-->
<div id="image_id">
<img src="" alt="image" />
</div>
2. Now we have to include jQuery and html2canvas.js.
1
2
3
<script type="text/javascript" src="http://html2canvas.hertzen.com/build/html2canvas
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></s
Note: The source link contains html2canvas v0.40. I recommend you to check for a newer
version and use it instead from the official html2canvas site.
3. Now add below script to convert your entire Div to an image.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<script>
html2Canvas[document.getElementById('mainDiv')],{
onrendered: function (canvas){
var imagedata = canvas.toDataURL('image/png');
var imgdata =imagedata.replace(/^data:image/(png|jpg);base64,/,"") ;
//ajax call to save image inside folder
$.ajax({
url: 'save_image.php',
data:
{
imgdata:imgdata
},
type: 'post',
success: function (response){
console.log(response);
$('#image_id img').attr('src' , response);
}
});
}
}
</script>
4. To save Div as an Image file.
1
2
3
4
5
6
7
$imagedata = base64_decode($_POST['imgdata']);
$filename = md5(uniqid(rand(),true));
//path where you want to upload image
$file = $_SERVER['DOCUMENT_ROOT'].'/uploads/'.$filename.'.png';
$imageurl = 'http://example.com/uploads/'.$filename.'.png';
file_put_contents($file,$imageurl);
echo $imageurl;
Thus, how entire Div took as image and can be saved on the server as well in .png format.
Advantages
1- Allows taking partial or full screenshot
2- Allows can render the screenshot on client without server interaction
3- Provide mechanism to take on-demand screenshot on the client side
4- Generates the screenshot as per display of the web page in the browser, in which
we are taking the screenshot.
Limitations
1- Pages, which have frames containing pages from sources other than the origin of
the main page, are not displayed properly. Frames are grayed out.
2- Many CSS3 properties are not yet supported.
3- If the page being captured already has canvas element rendering the image from the
source other than the source of the main page, the screenshot would not be able to display
the canvas element properly.

More Related Content

What's hot

AWS re:Invent 2016: Hybrid Architecture Design: Connecting Your On-Premises W...
AWS re:Invent 2016: Hybrid Architecture Design: Connecting Your On-Premises W...AWS re:Invent 2016: Hybrid Architecture Design: Connecting Your On-Premises W...
AWS re:Invent 2016: Hybrid Architecture Design: Connecting Your On-Premises W...
Amazon Web Services
 
Soal mid semester tlj 12
Soal mid semester tlj 12Soal mid semester tlj 12
Soal mid semester tlj 12
smk n 4 surakarta
 
soal latihan mikrotik
soal latihan mikrotik soal latihan mikrotik
soal latihan mikrotik
Roni Sugiyanto
 
Cloud of things (IoT + Cloud Computing)
Cloud of things (IoT + Cloud Computing)Cloud of things (IoT + Cloud Computing)
Cloud of things (IoT + Cloud Computing)
Zakaria Hossain
 
Android Tools for Qualcomm Snapdragon Processors
Android Tools for Qualcomm Snapdragon Processors Android Tools for Qualcomm Snapdragon Processors
Android Tools for Qualcomm Snapdragon Processors
Qualcomm Developer Network
 
M2M systems layers and designs standardizations
M2M systems layers and designs standardizationsM2M systems layers and designs standardizations
M2M systems layers and designs standardizations
FabMinds
 
Machine-to-Machine Communications
Machine-to-Machine CommunicationsMachine-to-Machine Communications
Machine-to-Machine Communications
Dr. Rajesh P Barnwal
 
4g technology seminar ppt
4g technology seminar ppt4g technology seminar ppt
4g technology seminar ppt
Shreya Thakur
 
5 g
5 g5 g
DDBMS
DDBMSDDBMS
5 g technology ppt
5 g technology ppt5 g technology ppt
5 g technology ppt
Karthik BT
 
5g wireless
5g wireless5g wireless
5g wireless
amangupta919
 
5g introduction_NR
5g introduction_NR5g introduction_NR
5g introduction_NR
Nitin George Thomas
 
5 g wireless technology
5 g wireless technology5 g wireless technology
5 g wireless technology
Praween Lakra
 
Chapter 5 - Komputasi Awan.pptx
Chapter 5 - Komputasi Awan.pptxChapter 5 - Komputasi Awan.pptx
Chapter 5 - Komputasi Awan.pptx
Tirtana M. Siregar ST.P, MT.
 
5G and Internet of Things (IoT)
5G and Internet of Things (IoT)5G and Internet of Things (IoT)
5G and Internet of Things (IoT)
Atifa Aqueel
 
MANAJEMEN BANDWITH.pptx
MANAJEMEN BANDWITH.pptxMANAJEMEN BANDWITH.pptx
MANAJEMEN BANDWITH.pptx
HendyPradanaKurniawa
 
Meraki Overview
Meraki OverviewMeraki Overview
Meraki Overview
Cloud Distribution
 
ATP-TJKT-BK 4.docx
ATP-TJKT-BK 4.docxATP-TJKT-BK 4.docx
ATP-TJKT-BK 4.docx
adis498548
 
5G Technology
5G Technology5G Technology

What's hot (20)

AWS re:Invent 2016: Hybrid Architecture Design: Connecting Your On-Premises W...
AWS re:Invent 2016: Hybrid Architecture Design: Connecting Your On-Premises W...AWS re:Invent 2016: Hybrid Architecture Design: Connecting Your On-Premises W...
AWS re:Invent 2016: Hybrid Architecture Design: Connecting Your On-Premises W...
 
Soal mid semester tlj 12
Soal mid semester tlj 12Soal mid semester tlj 12
Soal mid semester tlj 12
 
soal latihan mikrotik
soal latihan mikrotik soal latihan mikrotik
soal latihan mikrotik
 
Cloud of things (IoT + Cloud Computing)
Cloud of things (IoT + Cloud Computing)Cloud of things (IoT + Cloud Computing)
Cloud of things (IoT + Cloud Computing)
 
Android Tools for Qualcomm Snapdragon Processors
Android Tools for Qualcomm Snapdragon Processors Android Tools for Qualcomm Snapdragon Processors
Android Tools for Qualcomm Snapdragon Processors
 
M2M systems layers and designs standardizations
M2M systems layers and designs standardizationsM2M systems layers and designs standardizations
M2M systems layers and designs standardizations
 
Machine-to-Machine Communications
Machine-to-Machine CommunicationsMachine-to-Machine Communications
Machine-to-Machine Communications
 
4g technology seminar ppt
4g technology seminar ppt4g technology seminar ppt
4g technology seminar ppt
 
5 g
5 g5 g
5 g
 
DDBMS
DDBMSDDBMS
DDBMS
 
5 g technology ppt
5 g technology ppt5 g technology ppt
5 g technology ppt
 
5g wireless
5g wireless5g wireless
5g wireless
 
5g introduction_NR
5g introduction_NR5g introduction_NR
5g introduction_NR
 
5 g wireless technology
5 g wireless technology5 g wireless technology
5 g wireless technology
 
Chapter 5 - Komputasi Awan.pptx
Chapter 5 - Komputasi Awan.pptxChapter 5 - Komputasi Awan.pptx
Chapter 5 - Komputasi Awan.pptx
 
5G and Internet of Things (IoT)
5G and Internet of Things (IoT)5G and Internet of Things (IoT)
5G and Internet of Things (IoT)
 
MANAJEMEN BANDWITH.pptx
MANAJEMEN BANDWITH.pptxMANAJEMEN BANDWITH.pptx
MANAJEMEN BANDWITH.pptx
 
Meraki Overview
Meraki OverviewMeraki Overview
Meraki Overview
 
ATP-TJKT-BK 4.docx
ATP-TJKT-BK 4.docxATP-TJKT-BK 4.docx
ATP-TJKT-BK 4.docx
 
5G Technology
5G Technology5G Technology
5G Technology
 

Similar to Solution to capture webpage screenshot with html2 canvas.js for backend developers

Presentation Tier optimizations
Presentation Tier optimizationsPresentation Tier optimizations
Presentation Tier optimizations
Anup Hariharan Nair
 
Introduction of webpack 4
Introduction of webpack 4Introduction of webpack 4
Introduction of webpack 4
Vijay Shukla
 
Migrating your Web app to Virtual Reality
Migrating your Web app to Virtual RealityMigrating your Web app to Virtual Reality
Migrating your Web app to Virtual Reality
Denis Radin
 
Lazy load Website Assets
Lazy load Website AssetsLazy load Website Assets
Lazy load Website Assets
Chris Love
 
Building and deploying React applications
Building and deploying React applicationsBuilding and deploying React applications
Building and deploying React applications
Astrails
 
WEBPACK
WEBPACKWEBPACK
WEBPACK
home
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP Application
Andrew Rota
 
JavaScript Performance (at SFJS)
JavaScript Performance (at SFJS)JavaScript Performance (at SFJS)
JavaScript Performance (at SFJS)
Steve Souders
 
JavaScript Perfomance
JavaScript PerfomanceJavaScript Perfomance
JavaScript Perfomance
Anatol Alizar
 
Build Better Responsive websites. Hrvoje Jurišić
Build Better Responsive websites. Hrvoje JurišićBuild Better Responsive websites. Hrvoje Jurišić
Build Better Responsive websites. Hrvoje Jurišić
MeetMagentoNY2014
 
HTML GL - 60 FPS and amazing effects by rendering HTML/CSS in WebGL, framewor...
HTML GL - 60 FPS and amazing effects by rendering HTML/CSS in WebGL, framewor...HTML GL - 60 FPS and amazing effects by rendering HTML/CSS in WebGL, framewor...
HTML GL - 60 FPS and amazing effects by rendering HTML/CSS in WebGL, framewor...
Denis Radin
 
Planbox Backbone MVC
Planbox Backbone MVCPlanbox Backbone MVC
Planbox Backbone MVC
Acquisio
 
React JS .NET
React JS .NETReact JS .NET
React JS .NET
Jennifer Estrada
 
Improve WordPress load times with a CDN
Improve WordPress load times with a CDNImprove WordPress load times with a CDN
Improve WordPress load times with a CDN
Austin Gil
 
Web Performance Part 4 "Client-side performance"
Web Performance Part 4  "Client-side performance"Web Performance Part 4  "Client-side performance"
Web Performance Part 4 "Client-side performance"
Binary Studio
 
AtlasCamp 2015: How HipChat ships at the speed of awesome
AtlasCamp 2015: How HipChat ships at the speed of awesomeAtlasCamp 2015: How HipChat ships at the speed of awesome
AtlasCamp 2015: How HipChat ships at the speed of awesome
Atlassian
 
Портируем существующее Web-приложение в виртуальную реальность / Денис Радин ...
Портируем существующее Web-приложение в виртуальную реальность / Денис Радин ...Портируем существующее Web-приложение в виртуальную реальность / Денис Радин ...
Портируем существующее Web-приложение в виртуальную реальность / Денис Радин ...
Ontico
 
Html5
Html5Html5
Html5
mikusuraj
 
Keeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackKeeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and Webpack
Ignacio Martín
 
Canvas in html5 - TungVD
Canvas in html5 - TungVDCanvas in html5 - TungVD
Canvas in html5 - TungVD
Framgia Vietnam
 

Similar to Solution to capture webpage screenshot with html2 canvas.js for backend developers (20)

Presentation Tier optimizations
Presentation Tier optimizationsPresentation Tier optimizations
Presentation Tier optimizations
 
Introduction of webpack 4
Introduction of webpack 4Introduction of webpack 4
Introduction of webpack 4
 
Migrating your Web app to Virtual Reality
Migrating your Web app to Virtual RealityMigrating your Web app to Virtual Reality
Migrating your Web app to Virtual Reality
 
Lazy load Website Assets
Lazy load Website AssetsLazy load Website Assets
Lazy load Website Assets
 
Building and deploying React applications
Building and deploying React applicationsBuilding and deploying React applications
Building and deploying React applications
 
WEBPACK
WEBPACKWEBPACK
WEBPACK
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP Application
 
JavaScript Performance (at SFJS)
JavaScript Performance (at SFJS)JavaScript Performance (at SFJS)
JavaScript Performance (at SFJS)
 
JavaScript Perfomance
JavaScript PerfomanceJavaScript Perfomance
JavaScript Perfomance
 
Build Better Responsive websites. Hrvoje Jurišić
Build Better Responsive websites. Hrvoje JurišićBuild Better Responsive websites. Hrvoje Jurišić
Build Better Responsive websites. Hrvoje Jurišić
 
HTML GL - 60 FPS and amazing effects by rendering HTML/CSS in WebGL, framewor...
HTML GL - 60 FPS and amazing effects by rendering HTML/CSS in WebGL, framewor...HTML GL - 60 FPS and amazing effects by rendering HTML/CSS in WebGL, framewor...
HTML GL - 60 FPS and amazing effects by rendering HTML/CSS in WebGL, framewor...
 
Planbox Backbone MVC
Planbox Backbone MVCPlanbox Backbone MVC
Planbox Backbone MVC
 
React JS .NET
React JS .NETReact JS .NET
React JS .NET
 
Improve WordPress load times with a CDN
Improve WordPress load times with a CDNImprove WordPress load times with a CDN
Improve WordPress load times with a CDN
 
Web Performance Part 4 "Client-side performance"
Web Performance Part 4  "Client-side performance"Web Performance Part 4  "Client-side performance"
Web Performance Part 4 "Client-side performance"
 
AtlasCamp 2015: How HipChat ships at the speed of awesome
AtlasCamp 2015: How HipChat ships at the speed of awesomeAtlasCamp 2015: How HipChat ships at the speed of awesome
AtlasCamp 2015: How HipChat ships at the speed of awesome
 
Портируем существующее Web-приложение в виртуальную реальность / Денис Радин ...
Портируем существующее Web-приложение в виртуальную реальность / Денис Радин ...Портируем существующее Web-приложение в виртуальную реальность / Денис Радин ...
Портируем существующее Web-приложение в виртуальную реальность / Денис Радин ...
 
Html5
Html5Html5
Html5
 
Keeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackKeeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and Webpack
 
Canvas in html5 - TungVD
Canvas in html5 - TungVDCanvas in html5 - TungVD
Canvas in html5 - TungVD
 

More from eLuminous Technologies Pvt. Ltd.

Why business merchants should use headless commerce?
Why business merchants should use headless commerce?Why business merchants should use headless commerce?
Why business merchants should use headless commerce?
eLuminous Technologies Pvt. Ltd.
 
6 Ecommerce trends that guarantee sales success
6 Ecommerce trends that guarantee sales success6 Ecommerce trends that guarantee sales success
6 Ecommerce trends that guarantee sales success
eLuminous Technologies Pvt. Ltd.
 
How Digital Transformation can improve your customer experience
How Digital Transformation can improve your customer experienceHow Digital Transformation can improve your customer experience
How Digital Transformation can improve your customer experience
eLuminous Technologies Pvt. Ltd.
 
8 Technology challenges that enterprise businesses need to prepare for in 2020
8 Technology challenges that enterprise businesses need to prepare for in 20208 Technology challenges that enterprise businesses need to prepare for in 2020
8 Technology challenges that enterprise businesses need to prepare for in 2020
eLuminous Technologies Pvt. Ltd.
 
6 eCommerce trends that hold the key to eCommerce success in 2020
6 eCommerce trends that hold the key to eCommerce success in 20206 eCommerce trends that hold the key to eCommerce success in 2020
6 eCommerce trends that hold the key to eCommerce success in 2020
eLuminous Technologies Pvt. Ltd.
 
Benefits fo Digital Transformation to your business
Benefits fo Digital Transformation to your businessBenefits fo Digital Transformation to your business
Benefits fo Digital Transformation to your business
eLuminous Technologies Pvt. Ltd.
 
Why Digital agencies choose React JS for web development?
Why Digital agencies choose React JS for web development?Why Digital agencies choose React JS for web development?
Why Digital agencies choose React JS for web development?
eLuminous Technologies Pvt. Ltd.
 
Modern Data Integration Approach 2020
Modern Data Integration Approach 2020Modern Data Integration Approach 2020
Modern Data Integration Approach 2020
eLuminous Technologies Pvt. Ltd.
 
9 React Developer Tools to Create Better Apps Faster
9 React Developer Tools to Create Better Apps Faster9 React Developer Tools to Create Better Apps Faster
9 React Developer Tools to Create Better Apps Faster
eLuminous Technologies Pvt. Ltd.
 
Flutter VS React Native 2020 Which Cross-platfrom Mobile Framework is Right f...
Flutter VS React Native 2020 Which Cross-platfrom Mobile Framework is Right f...Flutter VS React Native 2020 Which Cross-platfrom Mobile Framework is Right f...
Flutter VS React Native 2020 Which Cross-platfrom Mobile Framework is Right f...
eLuminous Technologies Pvt. Ltd.
 
Progressive Web Apps - Quick Guide to Explore
Progressive Web Apps - Quick Guide to ExploreProgressive Web Apps - Quick Guide to Explore
Progressive Web Apps - Quick Guide to Explore
eLuminous Technologies Pvt. Ltd.
 
Mobile app development trends 2019
Mobile app development trends 2019Mobile app development trends 2019
Mobile app development trends 2019
eLuminous Technologies Pvt. Ltd.
 
New in iOS 11
New in iOS 11New in iOS 11
How to plan your web design?
How to plan your web design?How to plan your web design?
How to plan your web design?
eLuminous Technologies Pvt. Ltd.
 
The Design Process
The Design ProcessThe Design Process
Angular vs react | front end developer
Angular vs react | front end developerAngular vs react | front end developer
Angular vs react | front end developer
eLuminous Technologies Pvt. Ltd.
 
Node js | Back end developer
Node js | Back end developerNode js | Back end developer
Node js | Back end developer
eLuminous Technologies Pvt. Ltd.
 
Empower your business with RectJS Development | front end developer
Empower your business with RectJS Development | front end developerEmpower your business with RectJS Development | front end developer
Empower your business with RectJS Development | front end developer
eLuminous Technologies Pvt. Ltd.
 
Hire Pentaho Developer | BI Tools
Hire Pentaho Developer | BI ToolsHire Pentaho Developer | BI Tools
Hire Pentaho Developer | BI Tools
eLuminous Technologies Pvt. Ltd.
 
Latest mobile app development trends
Latest mobile app development trendsLatest mobile app development trends
Latest mobile app development trends
eLuminous Technologies Pvt. Ltd.
 

More from eLuminous Technologies Pvt. Ltd. (20)

Why business merchants should use headless commerce?
Why business merchants should use headless commerce?Why business merchants should use headless commerce?
Why business merchants should use headless commerce?
 
6 Ecommerce trends that guarantee sales success
6 Ecommerce trends that guarantee sales success6 Ecommerce trends that guarantee sales success
6 Ecommerce trends that guarantee sales success
 
How Digital Transformation can improve your customer experience
How Digital Transformation can improve your customer experienceHow Digital Transformation can improve your customer experience
How Digital Transformation can improve your customer experience
 
8 Technology challenges that enterprise businesses need to prepare for in 2020
8 Technology challenges that enterprise businesses need to prepare for in 20208 Technology challenges that enterprise businesses need to prepare for in 2020
8 Technology challenges that enterprise businesses need to prepare for in 2020
 
6 eCommerce trends that hold the key to eCommerce success in 2020
6 eCommerce trends that hold the key to eCommerce success in 20206 eCommerce trends that hold the key to eCommerce success in 2020
6 eCommerce trends that hold the key to eCommerce success in 2020
 
Benefits fo Digital Transformation to your business
Benefits fo Digital Transformation to your businessBenefits fo Digital Transformation to your business
Benefits fo Digital Transformation to your business
 
Why Digital agencies choose React JS for web development?
Why Digital agencies choose React JS for web development?Why Digital agencies choose React JS for web development?
Why Digital agencies choose React JS for web development?
 
Modern Data Integration Approach 2020
Modern Data Integration Approach 2020Modern Data Integration Approach 2020
Modern Data Integration Approach 2020
 
9 React Developer Tools to Create Better Apps Faster
9 React Developer Tools to Create Better Apps Faster9 React Developer Tools to Create Better Apps Faster
9 React Developer Tools to Create Better Apps Faster
 
Flutter VS React Native 2020 Which Cross-platfrom Mobile Framework is Right f...
Flutter VS React Native 2020 Which Cross-platfrom Mobile Framework is Right f...Flutter VS React Native 2020 Which Cross-platfrom Mobile Framework is Right f...
Flutter VS React Native 2020 Which Cross-platfrom Mobile Framework is Right f...
 
Progressive Web Apps - Quick Guide to Explore
Progressive Web Apps - Quick Guide to ExploreProgressive Web Apps - Quick Guide to Explore
Progressive Web Apps - Quick Guide to Explore
 
Mobile app development trends 2019
Mobile app development trends 2019Mobile app development trends 2019
Mobile app development trends 2019
 
New in iOS 11
New in iOS 11New in iOS 11
New in iOS 11
 
How to plan your web design?
How to plan your web design?How to plan your web design?
How to plan your web design?
 
The Design Process
The Design ProcessThe Design Process
The Design Process
 
Angular vs react | front end developer
Angular vs react | front end developerAngular vs react | front end developer
Angular vs react | front end developer
 
Node js | Back end developer
Node js | Back end developerNode js | Back end developer
Node js | Back end developer
 
Empower your business with RectJS Development | front end developer
Empower your business with RectJS Development | front end developerEmpower your business with RectJS Development | front end developer
Empower your business with RectJS Development | front end developer
 
Hire Pentaho Developer | BI Tools
Hire Pentaho Developer | BI ToolsHire Pentaho Developer | BI Tools
Hire Pentaho Developer | BI Tools
 
Latest mobile app development trends
Latest mobile app development trendsLatest mobile app development trends
Latest mobile app development trends
 

Recently uploaded

20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Zilliz
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
Pixlogix Infotech
 

Recently uploaded (20)

20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
 

Solution to capture webpage screenshot with html2 canvas.js for backend developers

  • 1. Solution to Capture Webpage Screenshot with Html2Canvas.js for Backend Developers Recently, I was to implement a web application requirement that the screenshot of a web page containing sketches done by users be captured, as the image is being saved. While this may seem trivial, a lot of intricacies is involved in building an image representation of the page as it is shown on the browser. However, when I tried to implement that, it wasn’t so easy as I expected. Some old ways offer ActiveX but it seems too outdated. Since there’s a bridge needed between client side and server, JS libraries are the best way.There’s a great library, html2canvas. It is told to be reverse-engineered version of Google Plus’ way of taking screenshots. Html2canvas.js is a JavaScript library that provides functionality for capturing a screenshot; the screenshot is built based on the information available on the page. The script traverses the DOM and builds a representation of the page alongside the images and the CSS styles present on the page, but it doesn’t render flash, java applets, and iframe. The html2canvas library can be downloaded here. When I first discovered this library, it took me a while to use for simplest implementation. I just wanted to visualize a div element. However, there was no single page to tell the whole path to follow, thus I had to combine various sources.
  • 2. Here have how you can easily use for taking a screenshot of a div. There are three libraries to import: 1. jquery.js 2. html2canvasjs 3. plugin.html2canvas.js It uses the jQuery plugin HTML2Canvas that converts a given page section to a graphic image on HTML5 canvas element and submits the captured image data to the server via AJAX. The script traverses through the DOM of the page it is loaded on. It gathers information on all the elements there, which it then uses to build a representation of the page. In other words, it does not actually take a screenshot of the page but builds a representation of it based on the properties it reads from the DOM. I need to convert entire div to image and save to our server in my web application. So I do provide some code below for your understanding purpose. 1. HTML code for Div which we have to capture as an image. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <div id="mainDiv"> Welcome to eLuminous Technologies <img src="logo.png" alt="Eluminous Technologies"> </div> <!-----------add image url after ajax response--> <div id="image_id"> <img src="" alt="image" /> </div> 2. Now we have to include jQuery and html2canvas.js.
  • 3. 1 2 3 <script type="text/javascript" src="http://html2canvas.hertzen.com/build/html2canvas <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></s Note: The source link contains html2canvas v0.40. I recommend you to check for a newer version and use it instead from the official html2canvas site. 3. Now add below script to convert your entire Div to an image. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <script> html2Canvas[document.getElementById('mainDiv')],{ onrendered: function (canvas){ var imagedata = canvas.toDataURL('image/png'); var imgdata =imagedata.replace(/^data:image/(png|jpg);base64,/,"") ; //ajax call to save image inside folder $.ajax({ url: 'save_image.php', data: { imgdata:imgdata }, type: 'post', success: function (response){ console.log(response); $('#image_id img').attr('src' , response); } }); } } </script> 4. To save Div as an Image file. 1 2 3 4 5 6 7 $imagedata = base64_decode($_POST['imgdata']); $filename = md5(uniqid(rand(),true)); //path where you want to upload image $file = $_SERVER['DOCUMENT_ROOT'].'/uploads/'.$filename.'.png'; $imageurl = 'http://example.com/uploads/'.$filename.'.png'; file_put_contents($file,$imageurl); echo $imageurl;
  • 4. Thus, how entire Div took as image and can be saved on the server as well in .png format. Advantages 1- Allows taking partial or full screenshot 2- Allows can render the screenshot on client without server interaction 3- Provide mechanism to take on-demand screenshot on the client side 4- Generates the screenshot as per display of the web page in the browser, in which we are taking the screenshot. Limitations 1- Pages, which have frames containing pages from sources other than the origin of the main page, are not displayed properly. Frames are grayed out. 2- Many CSS3 properties are not yet supported. 3- If the page being captured already has canvas element rendering the image from the source other than the source of the main page, the screenshot would not be able to display the canvas element properly.