Hola estimados, tengo un problema, necesito sacar un screenshot a la pagina que estoy visitando , la idea es esta:
Tengo un html de cartola y ese html lo tengo que pasar a pdf, ya lo hice y me quedo bien feo la conversion de html a pdf, asi que mi idea es :
sacar un pantallazo a la pagina con el boton exportar, esa imagen guardarla en el pdf y al exportar todo generar el pdf con el pantallazo del html generado, la cosa es que he intentado usar esa clase Robot para sacar el pantallazo y la cosa es que me sale una excepcion asumo que es porque no hay gui, como es una clase java que hace todo y es todo a nivel web (JSF) no me sale.
Si me dan cualquier pista, una mano, encantado estaría gracias de antemano.
///Datos////
Este es el erro que me sale :
GRAVE: java.awt.HeadlessException
Este es el metodo
/*Testeo de screenshot*/
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize(); //En esta linea lanza la excepcion
Robot robot = new Robot();
Rectangle rectangle = new Rectangle(0, 0, screenSize.width - 15, screenSize.height);
BufferedImage image = robot.createScreenCapture(rectangle);
ImageIO.write(image, "jpg", new File("c:\\mmgg.jpg"));
/*Fin testeo*/
Intente con esto y se me ve bien con el html de ejemplo, pero con el mio solo muestra texto =/
// file.write();
document.close();
file.close();
/* TESTING*/
String htmls = "<html>"
+ "<h1>:)</h1>"
+ "Hello World!<br>"
+ "<img src=\"http://img0.gmodules.com/ig/images/igoogle_logo_sm.png\">"
+ "</html>";
JLabel label = new JLabel(html);
label.setSize(5000,5000);
BufferedImage image = new BufferedImage(
label.getWidth(), label.getHeight(),
BufferedImage.TYPE_INT_ARGB);
{
// paint the html to an image
Graphics g = image.getGraphics();
g.setColor(Color.BLACK);
label.paint(g);
g.dispose();
}
// get the byte array of the image (as jpeg)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
byte[] bytes = baos.toByteArray();
ImageIO.write(image, "png", new File("C:/test.png"));
/* FIN TESTING */
Ignoren el tamaño del label :xD
Bueno, pude conseguir la imagen, pero el problema es que me queda bastante inestable el metodo porque a veces no muestra una tabla o alguna imagen y el footer no la muestra nunca, no se que sucede, lo hace a medias y como es algo serio no puedo dejarlo asi.
a ver si a alguien se le ocurre algo. Pego el codigo.
//JLabel label = new JLabel(html);
JEditorPane label = new JEditorPane();
// label.setContentType("text/html");
// label.setText(html);
// JTextPane label = new JTextPane();
label.setContentType("text/html");
label.setText(html);
label.setSize(2000, 1100);
BufferedImage image = new BufferedImage(
label.getWidth(), label.getHeight(),
BufferedImage.TYPE_INT_RGB);
{
// paint the html to an image
Graphics g = image.getGraphics();
// g.setColor(Color.BLACK);
label.paint(g);
g.dispose();
}
// get the byte array of the image (as jpeg)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
byte[] bytes = baos.toByteArray();
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
response.setContentType("application/jpg");
response.setHeader("Content-disposition", "attachment; filename=Testeo.png");
response.setContentLength(baos.size());
System.out.println("Tamaño BAOSPDF : " + baos.size());
ServletOutputStream out = response.getOutputStream();
ImageIO.write(image, "png", new FileOutputStream("C:/test.png"));
ImageIO.write(image, "png", out);