iText framework generating empty file (0 kb)

Asked

Viewed 288 times

1

I have a project that was generating the Pdfs normally, generating the texts and images in the file, it was a beauty, however, a while later it left unexpectedly to generate the file in the correct way, no matter what I do it will always generate the PDF with 0kb, completely empty, I did not touch the class and this appeared, I confess that I do not understand, I already removed it from Gradle and put again, I have created a new project just to test this framework and nothing to solve, will the problem be in Android Studio? He’s asking me to upgrade to version 1.2.1.1 or it will be the framework????

I’ll send a class from my test project, it’s very simple:

 public class TestePDF {

public void gerar() {
    String pathPdf = Environment.getExternalStorageDirectory()
            + "/Teste PDF/Notificações Teste/";

    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try{
        File dir = new File(pathPdf);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(dir, "Teste.pdf");
        FileOutputStream fOut = new FileOutputStream(file);
        document.open();
        Font bold = FontFactory.getFont("Times-Roman, Bold", 12, Font.BOLD);
        Paragraph p00 = new Paragraph("Teste Bold", bold);
        Paragraph p01 = new Paragraph("Teste sem bold");
        p00.setAlignment(Paragraph.ALIGN_LEFT);
        p01.setAlignment(Paragraph.ALIGN_LEFT);
        document.add(p00);
        document.add(Chunk.NEWLINE);
        document.add(p01);
        document.add(Chunk.NEWLINE);
        document.close();
        Log.i("PDF", pathPdf);
    }catch (Exception e){
        Log.e("Erro PDF", e.toString());
    }
}

}

Even so, only generates file with 0 kb.

GRADLE:

compile 'com.itextpdf:itextpdf:5.0.6'

1 answer

0

Well guys, problem solved, I hope it’s useful for someone, in my project, I must have erased a line, researched and noticed the missing:

PdfWriter.getInstance(document, fOut);

I need to add before this line:

document.open(); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.