Forum Discussion
916380172248
Sep 06, 2024Copper Contributor
Vlookup9
<!DOCTYPE html>
<html>
<head>
<title>How to Capture Screenshot of Page using JavaScript</title>
<link rel='stylesheet' href='form.css' type='text/css' />
</head>
<body>
<div class="phppot-container">
<h1>How to Capture Screenshot of Page using JavaScript</h1>
<p>
<button id="capture-screenshot">Capture Screenshot</button>
</p>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript"
src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<script type="text/javascript">
$('#capture-screenshot').click(function() {
const screenshotTarget = document.body;
html2canvas(screenshotTarget).then(canvas => {
// to image as png use below line
// const base64image = canvas.toDataURL("image/png");
// show the image in window use below line
// window.location.href = base64image;
// screenshot appended to the body as canvas
document.body.appendChild(canvas);
dataURL = canvas.toDataURL();
// to print the screenshot in console use below line
// console.log(dataURL);
// following line is optional and it is to save the screenshot
// on the server side. It initiates an ajax call
pushScreenshotToServer(dataURL);
});
});
function pushScreenshotToServer(dataURL) {
$.ajax({
url: "push-screenshot.php",
type: "POST",
data: {
image: dataURL
},
dataType: "html",
success: function() {
console.log('Screenshot pushed to server.');
}
});
}
</script>
</body>
</html>
No RepliesBe the first to reply