Module Lunchify
Package jku.se.export

Class PdfExporter

java.lang.Object
jku.se.export.PdfExporter

public class PdfExporter extends Object
Utility class for generating PDF documents using Apache PDFBox. Supports adding titles, paragraphs, tables, and images, and saving the result as a file.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initializes a new PDF document and sets the starting Y-position.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addImage(BufferedImage image, float x, float y, float width, float height)
    Insert an image into the PDF.
    void
    addImageAndMovePosition(BufferedImage image, float x, float width, float height, float gap)
    Helper method: Insert image and shift yPosition down so that the following text is underneath.
    void
    Adds a simple paragraph of text to the PDF using a regular font.
    void
    addTable(List<String> headers, List<List<String>> rows)
    Adds a table with headers and rows to the PDF.
    void
    Adds a title to the PDF using a bold font and larger font size.
    void
    end()
    Finalizes the current content stream (e.g., before saving or starting a new page).
    org.apache.pdfbox.pdmodel.PDPageContentStream
    Returns the current content stream used for writing to the PDF.
    org.apache.pdfbox.pdmodel.PDDocument
    Returns the internal PDF document.
    int
    Returns the current vertical writing position on the page.
    void
    saveToFile(String baseFileName)
    Saves the current PDF document to a file with a timestamped name in the Downloads folder.
    void
    setYPosition(int yPosition)
    Sets the current vertical writing position.
    void
    Starts a new page in the document and initializes a new content stream.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PdfExporter

      public PdfExporter()
      Initializes a new PDF document and sets the starting Y-position.
  • Method Details

    • getDocument

      public org.apache.pdfbox.pdmodel.PDDocument getDocument()
      Returns the internal PDF document.
      Returns:
      the PDDocument instance.
    • getContentStream

      public org.apache.pdfbox.pdmodel.PDPageContentStream getContentStream()
      Returns the current content stream used for writing to the PDF.
      Returns:
      the PDPageContentStream instance.
    • getYPosition

      public int getYPosition()
      Returns the current vertical writing position on the page.
      Returns:
      the Y-position.
    • setYPosition

      public void setYPosition(int yPosition)
      Sets the current vertical writing position.
      Parameters:
      yPosition - the Y-position to set.
    • startPage

      public void startPage() throws IOException
      Starts a new page in the document and initializes a new content stream.
      Throws:
      IOException - if the page or stream cannot be created.
    • end

      public void end() throws IOException
      Finalizes the current content stream (e.g., before saving or starting a new page).
      Throws:
      IOException - if closing the stream fails.
    • addTitle

      public void addTitle(String title) throws IOException
      Adds a title to the PDF using a bold font and larger font size.
      Parameters:
      title - the text to display as the title.
      Throws:
      IOException - if writing fails.
    • addParagraph

      public void addParagraph(String text) throws IOException
      Adds a simple paragraph of text to the PDF using a regular font.
      Parameters:
      text - the paragraph content.
      Throws:
      IOException - if writing fails.
    • addTable

      public void addTable(List<String> headers, List<List<String>> rows) throws IOException
      Adds a table with headers and rows to the PDF. Handles page breaks automatically if the table is too long for one page.
      Parameters:
      headers - List of column headers.
      rows - List of rows, each being a list of cell values.
      Throws:
      IOException - if writing fails.
    • addImage

      public void addImage(BufferedImage image, float x, float y, float width, float height) throws IOException
      Insert an image into the PDF.
      Parameters:
      image - The BufferedImage, which should be inserted
      x - X-position (from bottom left)
      y - Y-position (from bottom left)
      width - Width of the image in PDF points
      height - Height of the image in PDF points
      Throws:
      IOException - if insertion fails
    • addImageAndMovePosition

      public void addImageAndMovePosition(BufferedImage image, float x, float width, float height, float gap) throws IOException
      Helper method: Insert image and shift yPosition down so that the following text is underneath.
      Parameters:
      image - BufferedImage
      x - x-position
      width - Width of the image
      height - Height of the image
      gap - Distance down to the next element
      Throws:
      IOException
    • saveToFile

      public void saveToFile(String baseFileName) throws IOException
      Saves the current PDF document to a file with a timestamped name in the Downloads folder.
      Parameters:
      baseFileName - Base name of the file (without extension or timestamp).
      Throws:
      IOException - if saving fails.