Wednesday, 13 April 2016

Convert csv data to table in PDF documents

In this post, I am going to explain how to read csv file and write the data of csv file to a PDF file in table format. In my early posts, I explained how to read data from csv file and write data to csv file. Please go through above articles, if you want to know csv file handling.

For example,
employee.csv file has following data.

employee.csv
1,Hari Krishna,Gurram
2,Kiran Kumar,Darsi
3,Rama Krishna,Gurram
4,Gopi,Battu
5,Sudheer,Ganji

Following step-by-step procedure explains, how to convert csv data to a table in PDF document.

Step 1: Read csv file data as string


Step 2: Initialize PDDocument object.
PDDocument doc = new PDDocument();


Step 3: Initialize and create a landscape page.
PDPage page = new PDPage();
page.setMediaBox(new PDRectangle(PDRectangle.A4.getHeight(),PDRectangle.A4.getWidth()));


Step 4: Add pdf page to document.
doc.addPage(page);


Step 5: Initialize DataTable.

float margin = 10;
float tableWidth = page.getMediaBox().getWidth() - (2 * margin);
float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin);
float yStart = yStartNewPage;
float bottomMargin = 20;
BaseTable baseTable = new BaseTable(yStart, yStartNewPage,bottomMargin, tableWidth, margin, doc, page, true, true);
DataTable dataTable = new DataTable(baseTable, page);


Step 6: Add csv data to file.

/* Add csv data to table */
dataTable.addCsvToTable(data, hasHeader, separator);
baseTable.draw();


Step 7: Save the data to document.

File result = new File(destination);
doc.save(result);


Following is the complete working example.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.Optional;

import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import be.quodlibet.boxable.BaseTable;
import be.quodlibet.boxable.datatable.DataTable;

public class PDFUtil {
  final static Logger logger = LoggerFactory.getLogger(PDFUtil.class);

  /**
   * Read file data as string
   * 
   * @param url
   * @return on success read file data as string and return Optional<String>,
   *         else return Optional.empty().
   */
  private static Optional<String> getFileDataAsString(String filePath) {
    if (Objects.isNull(filePath)) {
      logger.error("filePath shouldn't be null");
      return Optional.empty();
    }

    File file = new File(filePath);

    try (InputStream in = new FileInputStream(file);) {
      return Optional.of(IOUtils.toString(in));
    } catch (IOException e) {
      logger.error("Unable to read file " + file);
      logger.error(e.getMessage());
      return Optional.empty();
    }
  }

  /**
   * 
   * @param csvFile
   * @param hasHeader
   *            : true represent csv file has header
   * @param separator
   * @param destination
   *            : Destination pdf file
   * @return On success return Optional<String>,else return Optional.empty().
   * @throws IOException
   */
  public static Optional<String> getPDFFromCSV(String csvFile,
      boolean hasHeader, char separator, String destination)
      throws IOException {

    if (Objects.isNull(csvFile)) {
      logger.error("file shouldn't be null");
      return Optional.empty();
    }

    Optional<String> fileData = getFileDataAsString(csvFile);

    if (!fileData.isPresent()) {
      logger.error("Unable to read file " + csvFile);
      return Optional.empty();
    }

    String data = fileData.get();

    /* Initialize PDDocument */
    try (PDDocument doc = new PDDocument()) {

      /* Initialize and create a landscape page */
      PDPage page = new PDPage();
      page.setMediaBox(new PDRectangle(PDRectangle.A4.getHeight(),
          PDRectangle.A4.getWidth()));

      /* Add page to PDDocument */
      doc.addPage(page);

      /* Initialize DataTable */
      float margin = 10;
      float tableWidth = page.getMediaBox().getWidth() - (2 * margin);
      float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin);
      float yStart = yStartNewPage;
      float bottomMargin = 20;
      BaseTable baseTable = new BaseTable(yStart, yStartNewPage,
          bottomMargin, tableWidth, margin, doc, page, true, true);
      DataTable dataTable = new DataTable(baseTable, page);

      /* Add csv data to table */
      dataTable.addCsvToTable(data, hasHeader, separator);
      baseTable.draw();

      File result;
      if (Objects.isNull(destination)) {
        result = new File("temp.pdf");
      } else {
        result = new File(destination);
      }

      doc.save(result);
      return Optional.of(result.getAbsolutePath());
    }

  }

}


import java.io.IOException;
import java.util.Optional;

public class Test {
  public static void main(String args[]) throws IOException {
    Optional<String> pdfFile = PDFUtil.getPDFFromCSV(
        "/Users/harikrishna_gurram/employee.csv", true, ',',
        "result.pdf");

    if (!pdfFile.isPresent()) {
      System.out.println("Error while processing, go through logs");
    }

    System.out.println("PDF file is saved at " + pdfFile.get());
  }
}







Previous                                                 Next                                                 Home

2 comments:

  1. what are the dependencies?

    ReplyDelete
    Replies
    1. You can refer this post for dependencies.
      https://self-learning-java-tutorial.blogspot.com/2016/03/introduction-to-apache-pdfbox.html

      Delete