Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

React.js use-react-screenshot Example to Take Screenshot of HTML With CSS and Download it as Image in Browser

Posted on February 13, 2023

 

 

Welcome folks today in this blog post we will be using the use-react-screenshot library to take screenshot of html with css element and download it as image file in browser using javascript. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new react.js project using the below commands as shown below

 

 

npx create-react-app sampleapp

 

 

cd sampleapp

 

 

And now we need to install the below library using the below command as shown below

 

 

npm i use-react-screenshot

 

 

And after that you will see the below directory structure of the react.js app as shown below

 

 

 

 

 

And now we need to edit the App.js file and copy paste the below code

 

 

App.js

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import './App.css';
import React, { createRef } from "react";
 
 
function App() {
  const ref = createRef(null);
 
  return (
    <div>
      <button onClick={downloadScreenshot}>Download screenshot</button>
      <div
        ref={ref}
        style={{
          border: "1px solid #ccc",
          background:'red',
          padding: "10px",
          marginTop: "20px"
        }}
      >
        <h1>John Williamson is the Captain<br></br>
        of New Zealand
        </h1>.
      </div>
    </div>
  );
}
 
export default App;

 

 

As you can see we have the simple html and css template and inside the div we have attached the ref attribute and we are importing the useRef at the top and it is initialized to null when we load the page. And then we have also attached custom styles to the div such as the background color and the margin,padding etc. And then we have the button to take the screenshot of the html div element and download it as attachment.

 

 

And now we will define the function once you hit the download button. So just modify the App.js file and copy paste the below code

 

 

App.js

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import './App.css';
import React, { createRef } from "react";
import { useScreenshot, createFileName } from "use-react-screenshot";
 
 
function App() {
  const ref = createRef(null);
  const [image, takeScreenShot] = useScreenshot({
    type: "image/jpeg",
    quality: 1.0
  });
 
  const download = (image, { name = "img", extension = "jpg" } = {}) => {
    const a = document.createElement("a");
    a.href = image;
    a.download = createFileName(extension, name);
    a.click();
  };
 
  const downloadScreenshot = () => takeScreenShot(ref.current).then(download);
 
  return (
    <div>
      <button onClick={downloadScreenshot}>Download screenshot</button>
      <div
        ref={ref}
        style={{
          border: "1px solid #ccc",
          background:'red',
          padding: "10px",
          marginTop: "20px"
        }}
      >
        <h1>John Williamson is the Captain<br></br>
        of New Zealand
        </h1>.
      </div>
    </div>
  );
}
 
export default App;

 

 

As you can see we have imported the library at the top and basically it’s a hook that we have initialized and inside it we are passing the mimetype of the captured image and the quality of the image. And then we are calling the hook function which is takeScreenshot() to actually capture the image and download it as attachment as shown below

 

 

 

Recent Posts

  • Android Java Project to Merge Multiple PDF Documents From Gallery Using iTextPDF Library
  • Android Java Project to Detect System Hardware & System CPU Info & Display inside TextView Widget
  • Android Java Project to Integrate Google OAuth2 Login & Logout System & Save User Info in SharedPreferences
  • Android Java Project to Export Raw Text to PDF Document Using iTextPDF Library
  • Android Java Project to Export Images From Gallery to PDF Document Using iTextPDF Library
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme