1

I was able to generate a pdf file using iText but when I open it I always get an error that the file seems to be damaged. I already added the library itextg-5.5.8.jar

Anyone know how to fix this?

Here's my code

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;


public class MainActivity extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
            try {
                String FILE = Environment.getExternalStorageDirectory().toString() +"/Report.pdf";
                Document document = new Document();
                PdfWriter.getInstance(document, new FileOutputStream(FILE));
                document.open();
                Paragraph paragraph1 = new Paragraph();
                paragraph1.add("Hello World!");
                document.add(paragraph1);
                document.close();
                //Log.d("OK", "done");
                //Toast.makeText(getApplicationContext(), "Generated Report!", Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            }
}
}

I uploaded the PDF here's the link. As you can see, it only contains the following bytes:

%PDF-1.4
%âãÏÓ
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Yuu
  • 619
  • 3
  • 13
  • 34
  • The iText-specific code looks OK. Can you share the PDF that is generated? – Bruno Lowagie Dec 03 '15 at 10:28
  • @BrunoLowagie I updated my post and as we can see the we can't preview the `pdf` file in `google docs` – Yuu Dec 03 '15 at 10:38
  • The file isn't public. People have to request access. Make the file public if you want people to take a look at it. – Bruno Lowagie Dec 03 '15 at 10:47
  • @BrunoLowagie sorry about that. I tried `itextg-5.5.7.jar` with the same code and I did generate the pdf file without a problem – Yuu Dec 03 '15 at 11:08
  • Your "PDF" only contains the PDF header, nothing else. Are you sure no exception or error is thrown? – mkl Dec 03 '15 at 11:18
  • The header is generated when `document.open()` is invoked. When you add the paragraph using the default font, you need access to the file `Helvetica.afm`. My guess is that you strip `itextg-5.5.7.jar` of all its resources when you create your APK. I am 99% sure that an exception is thrown saying [1.not.found.as.a.resource](http://itextpdf.com/question/why-do-i-get-message-saying-1notfoundasaresource). – Bruno Lowagie Dec 03 '15 at 11:31
  • 2
    I updated the question because it insinuates that iText creates corrupt PDFs. That's obviously not true. In this case, no PDF is generated: the generated file only contains the header bytes. – Bruno Lowagie Dec 03 '15 at 12:16
  • There does seem to be an issue with the recent 5.5.8 release of iTextG. We'll look into it asap. – rhens Dec 04 '15 at 18:30
  • @FrostyPinky There was a build problem with iTextG 5.5.8: the resources were in the jar, but they were all 0 bytes. This has been fixed and a proper 5.5.8 build is available for download. – rhens Dec 09 '15 at 16:48

0 Answers0