controller.PdfManager.java Source code

Java tutorial

Introduction

Here is the source code for controller.PdfManager.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 controller;

import java.io.IOException;
import java.io.Serializable;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import integration.CompetenceProfileDTO;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.List;
import javax.ejb.Stateless;

/**
 * This class will generate pdfs for the applicants application
 * 
 */
@Stateless
public class PdfManager implements Serializable {
    /**
     * This method will generate an pdf for the applicant
     * @param email the email of the user
     * @param competences the competences that the applican has added
     * @param dates the availability periods that the applicant added
     * @throws IOException 
     */
    public void downloadPDF(String email, List<CompetenceProfileDTO> competences, List<String> dates)
            throws IOException {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
        try {
            Font font1 = new Font(Font.FontFamily.HELVETICA, 30, Font.BOLD);
            Font font2 = new Font(Font.FontFamily.HELVETICA, 25, Font.UNDERLINE);
            Font font3 = new Font(Font.FontFamily.HELVETICA, 14);
            Document document = new Document();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);
            document.open();
            Paragraph h1 = new Paragraph("" + email + " Application", font1);
            h1.setAlignment(Element.ALIGN_CENTER);
            document.add(h1);
            //Add all competences
            document.add(new Paragraph("Competences", font2));
            for (CompetenceProfileDTO s : competences) {
                document.add(new Paragraph(
                        "Work: " + s.getName() + " Years of Experience: " + s.getYearsOfExperience() + " ", font3));
            }

            document.add(new Paragraph("Availability", font2));
            for (String ava : dates) {
                document.add(new Paragraph("" + ava + "", font3));
            }
            document.close();

            // setting some response headers
            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
            response.setHeader("Pragma", "public");
            response.setHeader("Content-Disposition", "inline; filename=\"" + email + "\".pdf");
            response.setContentType("application/pdf");

            // the contentlength
            response.setContentLength(baos.size());
            // write ByteArrayOutputStream to the ServletOutputStream
            OutputStream os = response.getOutputStream();
            baos.writeTo(os);
            os.flush();
            os.close();
        } catch (DocumentException e) {
            throw new IOException(e.getMessage());
        }
    }
}