2
I have a project that I need to make a report for each code. And save it on disk in PDF format. After generating the report, I don’t know how to pass the page (generated) to Servlet and save it in PDF.
I was able to upload the html to Servlet, now I need to know how to convert the tags (html) so that java can interpret them and transform them into PDF.
Servlet
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.drz.paraopeba.servlets;
import com.itextpdf.text.Document;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author douglas
*/
@WebServlet(name = "salvarPDF", urlPatterns = {"/salvarPDF"})
public class salvarPDF extends HttpServlet {
private static final String SAVE_DIR = "relatorios";
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String html = request.getParameter("conteudo");
try {
String appPath = request.getServletContext().getRealPath("");
String savePath = appPath + SAVE_DIR + File.separator;
OutputStream file = new FileOutputStream(new File(savePath + "teste.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(html));
document.close();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Can you be more specific about what for each code? Javascript is required?
– dougg0k
Yes, the content generation part is all ok, the HTML page will display the report to be printed / saved. Now I don’t know how to send this page to Servlet and turn it into PDF.
– Douglas Dreer
Javascript for Java I don’t know how, but only in java, there are some libs to parse html code and generate a pdf. One of them is iText.
– dougg0k
The Javascript / jQuery part is ready, I need to know how to take this result (or the current page that was generated) and send it to JAVA and turn it into PDF. This is one of the reports, I need to carry out more 3000 others.
– Douglas Dreer
@Douglasgaldino managed to send the content of the page (tags) via $.ajax, now I need to know if it is possible to convert and save to PDF.
– Douglas Dreer
Spitting out the HTML in the browser and the user generating the PDF is not a solution? If not, I suggest you look at a reporting framework like Jasper, iReports tool or something like...
– Thiesen
@Thiesen Incredibly not, as imagine a 'small' report of 3,400 items. =)
– Douglas Dreer