How to convert CSV to PDF in Android

2 minute read
Janhavi blog

They say that a simplest feature can go a long way sometimes. And we believe in that a 100%. At Obvious, we’ve been working on Simple, an app that helps hypertension patients and supports diabetes programs, for nearly four years. And when you’ve been working on a project for that long, multiple feature requests keep pouring in. One such feature request was to build a CSV to PDF converter, to enable staff nurses to download details of hypertensive or diabetic patients who were due for a visit.

As we set out to build this, we realised that there weren’t any direct libraries out there which did this. This therefore is a small how-to article that explains how we approached this.

We decided to divide and conquer — we handled the reading of the CSV data and PDF conversion separately. Our approach was to:

  1. Read the CSV file as array of strings separated by commas
  2. Create tables using the array and write that data in to a PDF document

Implementation

    1. Read the CSV file as an array of strings separated by commas

Library used: OpenCSV

private fun readCsvFile(file: File): List<Array<String>> {
    val reader = CSVReader(FileReader(file))
    return reader.readAll()
  }


    2. Create tables using the array and write that data in to a PDF document

Library used: IText7

Note: Min API level is 24

Open PDF document in writing mode with PdfWriter writing to the passed filename and with default writer properties.

    val pdfDoc = PdfDocument(PdfWriter(outputFile)) 


  • Construct a Table with specified number of columns and font size.

val table = Table(numberOfColumns).setFontSize(8f)


  • For each element in the entry add cells to the table.

entries
        .drop(1) // First element is the document name, therefore it should not be part of the table
        .flatMap { it.toList() }
        .forEach(table::addCell)


Document(pdfDoc, PageSize.A4.rotate()).run {
      add(Paragraph(documentName))
      add(table)
      close()
    }


All together;

private fun createPdfDocument(outputFile: File, entries: List<Array<String>>) {
    val pdfDoc = PdfDocument(PdfWriter(outputFile)) 
    val documentName = entries[0].joinToString()
    val numberOfColumns = entries[1].size

    val table = Table(numberOfColumns)
        .setFontSize(8f)

    entries
        .drop(1) 
        .flatMap { it.toList() }
        .forEach(table::addCell)

    Document(pdfDoc, PageSize.A4.rotate()).run {
      add(Paragraph(documentName))
      add(table)
      close()
    }


If you want to take a look at the complete implementation, check here: CsvToPdfConverter.kt.

Conclusion

The CSV to PDF converter that we created using this approach has been out in production for a few months and has been working as expected. The two libraries that we’re using here are also quite well maintained so we can know that this a safe way to convert your CSV files to PDF.

Lastly, since Simple is an open source project, you can explore the codebase to learn more about some of the other interesting things that we’ve been working on.

Header illustration from Freepik