简体   繁体   中英

How can I take a screenshot of a whole web page?

I want to take the screenshot of Jsp page in the browser. I had googled a lot. Everyone is pointing to java.awt.Robot functionality. It is great. But what i need is i want the screenshot of the full web page which is also inside the scrollable area of the browser window. Moreover i want only the webpage content not the status bar and other tabs and menus on the browser. I had used the following code.

public class ScreenCapture {

public void TakeCapture() 
{
try
{
        Robot robot = new Robot();
        String format = "jpg";
        String fileName = "D:\\PDFTest\\PartialScreenshot." + format;

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle captureRect = new Rectangle(0, 0, screenSize.width , screenSize.height);
        BufferedImage screenFullImage = robot.createScreenCapture(captureRect);
        ImageIO.write(screenFullImage, format, new File(fileName));


     Document document = new Document();
    String input = "D:\\PDFTest\\PartialScreenshot.jpg"; 
    String output = "D:\\PDFTest\\PartialScreenshot.pdf";
    try {
      FileOutputStream fos = new FileOutputStream(output);
      PdfWriter writer = PdfWriter.getInstance(document, fos);
      writer.open();
      document.open();
      document.add(Image.getInstance(input));
      document.close();
      writer.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
}

catch (AWTException | IOException ex) {
        System.err.println(ex);

    }
}

public String getTakeCapture() {
    return getTakeCapture();
}

Is there a way to take the screen shot of the full JSP webpage that is viewing in the browser.(Along the content inside the scrollable window) and then I have to convert this screenshot into PDF. Don't tell me the ways to directly convert it into the PDF using FlyingSaucer as it's not working in my case.

This is not possible in pure Java.

However you can add the html2canvas library to your JSP page. You can then use Javascript to submit the canvas image to your servlet and process it as you please.

See the following question and answer that deals with similar problem: How to upload a screenshot using html2canvas?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM