Boa tarde, estou fazendo uma aplicação web e quero abrir um documento pdf através do Java. Deve abrir em todos os OS’s.
Utilizei o java.awt.Desktop, porem ele só funciona quando a aplicação esta em local, quando subo no servidor GlassFish ele da HandlessException. Alguém tem alguma ideia de como posso abrir o documento?
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
@WebServlet(urlPatterns = { "/relatorio" })
public class RelatorioController extends HttpServlet {
private static final long serialVersionUID = 1L;
// String diretorioUpload = System.getProperty("java.io.tmpdir");
String diretorioUpload = "/home/relatorio/";
public RelatorioController() {
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sessionOperador = getServletContext();
switch (request.getRequestURI()) {
case "/NetCenter/relatorio":
criarPdf();
break;
}
}
public void criarPdf() {
System.out.println("Inicio");
Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
try {
// step2
PdfWriter.getInstance(document, new FileOutputStream(diretorioUpload + "arquivopdf.pdf"));
// step3
document.open();
// step4
String[] bogusData = { "M0065920", "SL", "FR86000P", "PCGOLD", "119000", "96 06", "2001-08-13", "4350",
"6011648299", "FLFLMTGP", "153", "119000.00" };
int NumColumns = 12;
PdfPTable datatable = new PdfPTable(NumColumns);
int[] headerwidths = { 9, 4, 8, 10, 8, 11, 9, 7, 9, 10, 4, 10 }; // percentage
datatable.setWidths(headerwidths);
datatable.setWidthPercentage(100); // percentage
datatable.getDefaultCell().setPadding(3);
datatable.getDefaultCell().setBorderWidth(2);
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
datatable.addCell("Clock #");
datatable.addCell("Trans Type");
datatable.addCell("Cusip");
datatable.addCell("Long Name");
datatable.addCell("Quantity");
datatable.addCell("Fraction Price");
datatable.addCell("Settle Date");
datatable.addCell("Portfolio");
datatable.addCell("ADP Number");
datatable.addCell("Account ID");
datatable.addCell("Reg Rep ID");
datatable.addCell("Amt To Go ");
datatable.setHeaderRows(1); // this is the end of the table header
datatable.getDefaultCell().setBorderWidth(1);
for (int i = 1; i < 750; i++) {
if (i % 2 == 1) {
datatable.getDefaultCell().setGrayFill(0.9f);
}
for (int x = 0; x < NumColumns; x++) {
datatable.addCell(bogusData[x]);
}
if (i % 2 == 1) {
datatable.getDefaultCell().setGrayFill(1);
}
}
document.add(datatable);
} catch (Exception de) {
System.out.println(de);
}
// step5
document.close();
try {
Desktop desktop = Desktop.getDesktop();
desktop.open(new File(diretorioUpload + "arquivopdf.pdf"));
} catch (Exception e) {
System.out.println(e);
}
}
}