com.wipro.srs.service.PrintTicket.java Source code

Java tutorial

Introduction

Here is the source code for com.wipro.srs.service.PrintTicket.java

Source

package com.wipro.srs.service;

import java.io.FileOutputStream;
import java.io.StringReader;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.wipro.srs.bean.ReservationBean;
import com.wipro.srs.dao.ReservationDAO;

/**
*
* @author Rajesh Kumar
* @version 1.0,May 10,2015
* @since 1.0
*/
@Service
public class PrintTicket {
    private static String FILE = "f:/TicketPdf.pdf";
    private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED);
    private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);

    @Autowired
    ReservationDAO resDao;

    @Transactional
    public void generatePDF(String res) {
        try {

            ReservationBean rb = resDao.findByResID(res);

            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(FILE));
            document.open();
            addMetaData(document);
            addTitlePage(document);
            addContent(document, rb);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // iText allows to add metadata to the PDF which can be viewed in your Adobe
    // Reader
    // under File -> Properties
    private static void addMetaData(Document document) {
        document.addTitle("Maverick Ship services ");
        document.addSubject("Ticket");
        document.addAuthor("Maverick Ship services");
        document.addCreator("Maverick Ship services");
        document.addCreationDate();
    }

    private void addTitlePage(Document document) throws DocumentException {
        Paragraph preface = new Paragraph();
        // We add one empty line
        addEmptyLine(preface, 1);
        // Lets write a big header
        preface.add(new Paragraph("Title of the document", catFont));

        addEmptyLine(preface, 1);
        // Will create: Report generated by: _name, _date
        preface.add(new Paragraph("Ticket generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                smallBold));
        addEmptyLine(preface, 3);
        preface.add(new Paragraph("This is an autogenerated document", smallBold));

        addEmptyLine(preface, 8);

        preface.add(new Paragraph(
                "This document is a preliminary version and not subject to your license agreement or any other agreement with MaverickShipServices.com ;-).",
                redFont));

        document.add(preface);
        // Start a new page
        document.newPage();
    }

    private void addContent(Document document, ReservationBean rb) throws DocumentException {

        try {
            HTMLWorker htmlWorker = new HTMLWorker(document);
            String str = "<%@ page language='java' contentType='text/html; charset=ISO-8859-1'pageEncoding='ISO-8859-1'%> <%@taglib prefix='s' uri='/struts-tags' %>"
                    + " <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'> "
                    + "<html><head><meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'></head>"
                    + "<body>" + "<br><br><br><br><br><br><br><br>" + "<table border='1.0' align='center'>"
                    + "<tr><th>Reservation ID</th>" + "<th>Vehicle ID</th>" + "<th>Booking Date</th>"
                    + "<th>Journey Date</th>" + "<th>Total Fare(in Rs)</th>" + "<th>Booking Status</th></tr>"
                    + "<tr><td>" + rb.getReservationID() + "</td>" + "<td>" + rb.getUserID() + "</td>" + "<td>"
                    + rb.getBookingDate() + "</td>" + "<td>" + rb.getNoOfSeats() + "</td>" + "<td>"
                    + rb.getBookingStatus() + "</td>" + "<td>" + rb.getTotalFare() + "</td>" + "<td></td></tr>"
                    + "</table>" + "</body></html>";
            htmlWorker.parse(new StringReader(str));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }

    }
}