com.hotaviano.tableexporter.pdf.PDFExporter.java Source code

Java tutorial

Introduction

Here is the source code for com.hotaviano.tableexporter.pdf.PDFExporter.java

Source

/*
 * This software is a html table text convert to conventional formats
 * Copyright (C) 2014  Melo, Hotaviano
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.hotaviano.tableexporter.pdf;

import com.hotaviano.tableexporter.DefaultStringTableParser;
import com.hotaviano.tableexporter.ExporterInterface;
import com.hotaviano.tableexporter.StringTableParser;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;

/**
 * Implementation of PDF exporter
 * 
 * @author Melo, Hotaviano <melo.hotaviano@gmail.com>
 */
public class PDFExporter implements ExporterInterface {

    private final StringTableParser parser = new DefaultStringTableParser();

    @Override
    public byte[] export(String htmlTable) throws DocumentException {
        com.lowagie.text.Document doc = new com.lowagie.text.Document();

        Document document = null;
        try {
            document = parser.parse(htmlTable);
        } catch (JDOMException | IOException ex) {
            throw new DocumentException(ex);
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        PdfWriter.getInstance(doc, out);

        doc.open();

        PdfPTable table = new PdfPTable(columnLength(document));

        List<PdfPCell> cells = getHeaders(document);
        for (PdfPCell cell : cells) {
            table.addCell(cell);
        }

        List<String> data = getBodyValues(document);
        for (String item : data) {
            table.addCell(new PdfPCell(new Phrase(item)));
        }

        doc.add(table);
        doc.close();

        return out.toByteArray();
    }

    private int columnLength(Document document) {
        Element child = document.getRootElement().getChild("thead");
        return child.getChild("tr").getChildren().size();
    }

    private List<PdfPCell> getHeaders(Document document) {
        List<PdfPCell> result = new ArrayList<>();

        Element tr = document.getRootElement().getChild("thead").getChildren().get(0);

        for (Element th : tr.getChildren()) {
            PdfPCell cell = new PdfPCell(new Phrase(th.getText(), new Font(Font.BOLD)));
            result.add(cell);
        }

        return result;
    }

    private List<String> getBodyValues(Document document) {
        List<String> result = new ArrayList<>();

        List<Element> trs = document.getRootElement().getChild("tbody").getChildren();

        for (Element tr : trs) {
            for (Element td : tr.getChildren()) {
                result.add(td.getText());
            }
        }

        return result;
    }

}