Skip to content
Advertisement

iText PDF: the document has no pages

I’m trying to do a date range based filter to export a report into a PDF; however, when I click to export the PDF, I get the following message: ExceptionConverter: java.io.IOException: The document has no pages. Here’s my PDF class’ code:

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

public class PDFPrinter {
    private String date1;
    private String date2;
    public void printpdf(){
    try {
            
            String file_name = JOptionPane.showInputDialog("Digite o nome do arquivo: ");
            String file_place = "C:\";
            String file_name_and_place = "C:\File\" + file_name + ".pdf";
            Document document = new Document();           
            PdfWriter.getInstance(document,new FileOutputStream(file_name_and_place));
            document.open();
            ConnectionFactory myConn = new ConnectionFactory();
            Connection connection = myConn.getConnection();
            PreparedStatement ps = null;
            ResultSet rs = null;
            String query = "SELECT * FROM tb_pacientes";
            ps = connection.prepareStatement(query);
            rs=ps.executeQuery();
            
            while(rs.next()){
                PdfPTable table = new PdfPTable(3);
        
                Paragraph para = new Paragraph(rs.getString("UID")+" "+rs.getString("Name")+" "+rs.getString("Address")+" "+rs.getString("Age")+" "+rs.getString("Frontline")+" "+rs.getString("VaccinationDate")+" "+rs.getString("Priority"));
                document.add(para);
                document.add(new Paragraph(" "));
                
            }
            document.close();
            JOptionPane.showMessageDialog(null, "Seu arquivo foi criado com sucesso em "+file_name_and_place);
        }         
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Ocorreu um erro, tente novamente, por favor.");
            e.printStackTrace();
        }
}
    public void printPDFWithFilter(String date1, String date2){
        this.date1 = date1;
        this.date2 = date2;
        ConnectionFactory factory= new ConnectionFactory();
        try(Connection c = factory.getConnection()){
            String file_name = JOptionPane.showInputDialog("Digite o nome do arquivo: ");
            String file_place = "C:\";
            String file_name_and_place = "C:\File\" + file_name + ".pdf";
            Document document = new Document();           
            PdfWriter.getInstance(document,new FileOutputStream(file_name_and_place));
            document.open();
            ConnectionFactory myConn = new ConnectionFactory();
            Connection connection = myConn.getConnection();
            PreparedStatement ps = null;
            ResultSet rs = null;
            String query = "SELECT * FROM tb_pacientes WHERE VaccinationDate BETWEEN ? AND ?";
                         
            ps = connection.prepareStatement(query);
            ps.setString(1, "%" +date1+ "%");
            ps.setString(2, "%" +date2+ "%");
            rs=ps.executeQuery();            
            while(rs.next()){
                PdfPTable table = new PdfPTable(3);        
                Paragraph para = new Paragraph(rs.getString("UID")+" "+rs.getString("Name")+" "+rs.getString("Address")+" "+rs.getString("Age")+" "+rs.getString("Frontline")+" "+rs.getString("VaccinationDate")+" "+rs.getString("Priority"));
                document.add(para);
                document.add(new Paragraph(" "));                
            }
            document.close();
            JOptionPane.showMessageDialog(null, "Seu arquivo foi criado com sucesso em "+file_name_and_place);
        }
        catch(Exception e){
            e.printStackTrace();
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            String sStackTrace = sw.toString(); 
            System.out.println(sStackTrace);
            if (sStackTrace == "ExceptionConverter: java.io.IOException: The document has no pages."){
              JOptionPane.showMessageDialog (null,"Selecione as datas novamente; com as configurações atuais, o documento não teria páginas.");
            }
            else{
              JOptionPane.showMessageDialog(null, "Ocorreu um erro ao filtrar o PDF; por favor, tente novamente.");  
            }            
        }
    }
}

And here’s the code of the PDF generation button:

   private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  String date1 = jDateChooser1.getDate().toString();
  String date2 = jDateChooser2.getDate().toString();
  PDFPrinter pdfp = new PDFPrinter();
  pdfp.printPDFWithFilter(date1, date2);
    }  

                                  

I believe there’s a problem with SQL and JDateChooser, since its date format appears to be incompatible with SQL’s DATE format, but I’m not sure about it and I haven’t found any ways to change it, so, I’m here looking for help.

Advertisement

Answer

  1. It seems that your SQL request doesn’t return any row,

  2. Put your PdfTable(3) out of your while();

Advertisement