How to save an html page in java

Asked

Viewed 1,207 times

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?

  • 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.

  • 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.

  • 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.

  • @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.

  • 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 Incredibly not, as imagine a 'small' report of 3,400 items. =)

Show 2 more comments

1 answer

1


An option for PDF generation would be to generate a report even using iReports or Jasper.

If this is not an option, Voce can venture into libraries that convert HTML to PDF, some cases here

I don’t really recommend using this type of library, as PDF generation is usually not very consistent. Each of these libraries interprets the CSS in a different way, and some cases, it doesn’t even interpret. The end result usually gets coarse and ugly.

If your HTML is simple enough, maybe Voce can minimize these problems.

  • Well, I was able to take one more step and I was able to open the file, I get the html, but it’s not finding the path of accepting the image paths. I updated the codes.

  • I think Voce will have to download these images and put in a local path, parse the html for that local path, and then do the PDF generation by Htmlworker. Almost there!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.