coreservlets.reportPDF.java Source code

Java tutorial

Introduction

Here is the source code for coreservlets.reportPDF.java

Source

/*
 * 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 coreservlets;

import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.w3c.dom.Document;

/**
 *
 * @author mfaeez4
 */
public class reportPDF extends HttpServlet {

    /**
     * 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()) {
            response.setContentType("application/pdf");
            /** create Object Of document */
            Document document = new Document(PageSize.FLSE);
            /** set margin of page */
            document.setMargins(1.25F, 0.75F, 5F, 0.75F);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            PdfWriter pdfWriter = PdfWriter.getInstance(document, baos);
            document.open();
            /** Table For header address */
            PdfPTable headerTable = new PdfPTable(3);
            headerTable.setWidthPercentage(91);
            /** Add company Address*/
            /** First Cell  For address*/
            PdfPCell headerCell = new PdfPCell();
            headerCell.addElement(new Paragraph("My comapany"));
            /** add cell to headerTable*/
            headerTable.addCell(headerCell);
            /** Second cell For CompanyLogo */
            headerCell = new PdfPCell();
            /** get context object */
            ServletContext context = request.getSession().getServletContext();
            /** get Real  Path of image */
            String path = context.getRealPath(login.getPath());
            /** set image to cell*/
            Image img = Image.getInstance(path);
            headerCell.setImage(img);
            /** add cell to headerTable*/
            headerTable.addCell(headerCell);
            /** Third cell for lable of document **/
            headerCell = new PdfPCell();
            String allLedger = "ALL LEDGER";
            headerCell.addElement(new Paragraph(
                    allLedger + "\n" + "From Date:" + session.getAttribute("firstDate") + "\nTo Date:"
                            + session.getAttribute("lastDate"),
                    new Font(Font.TIMES_ROMAN, 10f, Font.BOLD, Color.black)));

            /** add cell to headerTable*/

            headerTable.addCell(headerCell);
            /** add table to document */

            document.add(headerTable);
            document.close();
            /** Don't know why??*/
            response.setContentLength(baos.size());
            /** comment it if u don't want to download pdf*/
            //response.addHeader("Content-Disposition", "attachment; filename=\"ledgers.pdf\"");

            /** get out put Stream*/
            out = response.getOutputStream();
            /** write to--- Don't know*/
            baos.writeTo(out);
            /** Clear OutputStream*/
            out.flush();
            out.close();

        }
    }

    // <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 {
        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>

}