generate 0 byte pdf with droidtext

1k Views Asked by At

I am working on an Android app need to generate a pdf report. But the report.pdf I get is 0 byte and can not be opened. I use iText 5.4.3 before and had the same problem. (Now I use droidText). droidText.0.5.jar was imported into a lib folder and added into Android Private Libraries (which is on the build path).

Here is my code to generate a simple hello world.

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public void onGeneratePDF(View v){
        String dir = Environment.getExternalStorageDirectory().getAbsolutePath();
        dir = dir+"/Report/Report.pdf";
        try {
              Document document = new Document(PageSize.LETTER.rotate());
              PdfWriter.getInstance(document, new FileOutputStream(dir));
              document.open();
              document.add(new Paragraph("test"));        
              document.close();
        } catch (Exception e) {
              e.printStackTrace();
        }
    }

Manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

And the log

08-08 10:06:59.636: W/System.err(16633): Helvetica not found as resource.
(The *.afm files must exist as resources in the package com.lowagie.text.pdf.fonts)
08-08 10:06:59.641: W/System.err(16633): ExceptionConverter: java.io.IOException: 
The document has no pages.

Anyone can help ? Thanks in advance :))

1

There are 1 best solutions below

3
On BEST ANSWER

I am the lowagie that is mentioned in your error message. Note that DroidText is software that isn't endorsed by me. It was distributed by people who aren't affiliated with me without me knowing anything about it.

It was also based on a version of iText that is considered obsolete. Read http://lowagie.com/itext2

Please use an official Android port of iText, such as iTextG: http://sourceforge.net/projects/itextg/

By the way: the error is telling you that the file Helvetica.afm isn't package with your APK. You should explicitly add com/itextpdf/text/pdf/fonts/Helvetica.afm to your APK so that iText can find it.