Search code examples
javamysqlnetbeanspdfboxboxable

Trying to print ResultSet to PDF file


I discovered this API called Boxable that also uses PDFBox, now I'm trying to implement the examples online but can't make it work. Here is my code.

public static ArrayList <String[]> result = new ArrayList<String[]>();

I just copied this function like on the example

private static PDPage addNewPage(PDDocument doc) {
    PDPage page = new PDPage();
    doc.addPage(page);
    return page;
}

These are part of my main function

For my Query execution

        Connection con = null;
        PreparedStatement ps = null;
        String query = "SELECT * FROM tablename";
        try{
            con = getConnection();
            ps = con.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            int columnCount = rs.getMetaData().getColumnCount();
            while(rs.next()){
                String[] row = new String[columnCount];
                for (int i=0;i<columnCount;i++){
                    row[i] = rs.getString(i+1);
                }
                result.add(row);
            }
        } catch (SQLException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }

Part for generating the Table

        try{
            float margin = 10;
            PDDocument doc = new PDDocument();
            PDPage page = addNewPage(doc);
            float tableWidth = page.getMediaBox().getWidth()-(2*margin);
            float yStartNewPage = page.getMediaBox().getHeight()-(2*margin);
            boolean drawContent = true;
            boolean drawLines =  true;
            float yStart = yStartNewPage;
            float bottomMargin = 70;
            BaseTable table = new BaseTable(yStart,yStartNewPage,bottomMargin,tableWidth,margin,doc,page,drawLines,drawContent);
            Row<PDPage> headerRow = table.createRow(15f);
            Cell<PDPage> cell = headerRow.createCell(100,"Sample Table");
            cell.setFont(PDType1Font.HELVETICA_BOLD);
            cell.setFillColor(Color.BLACK);
            table.addHeaderRow(headerRow);
            for (String[] print : result){
                Row<PDPage> row = table.createRow(10f);
                cell = row.createCell((100/3.0f)*2,print[0]);
                for (int i=1;i<print.length;i++){
                    cell = row.createCell((100/9f),print[i]);
                }
            }
            table.draw();
            doc.save(new File("java\\sample.pdf"));
            doc.close();
        } catch (IOException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }

The compiler gives me this Exception

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at be.quodlibet.boxable.utils.FontUtils.<clinit>(FontUtils.java:23)
at be.quodlibet.boxable.Cell.<init>(Cell.java:110)
at be.quodlibet.boxable.Cell.<init>(Cell.java:71)
at be.quodlibet.boxable.Row.createCell(Row.java:51)
at connect.NewClass.main(NewClass.java:115)

The said line on 115 is Cell<PDPage> cell = headerRow.createCell(100,"Sample Table");

I really don't have an idea what is wrong, and still new with Java. Also tried reading the code of the Class but can't detect the error. Will really appreciate your inputs about this.


Solution

  • NoClassDefFoundError means that you do not have that referenced dependency in your classpath. This is typically resolved by using a dependency manager like Maven or by copying the jar manually into your project root.

    Specifically, in your question, you are missing the org.slf4j.LoggerFactory package at compile time.