0

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 :))

codermomo
  • 23
  • 1
  • 1
  • 6

1 Answers1

1

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Hi Lowagie, what I used before is iTextG, but the same problem. BTW, is that possibly because iText need a license ? – codermomo Aug 08 '13 at 15:10
  • No, as explained: you need to make sure that either com/itextpdf/text/pdf/fonts/*.afm (iTextG) or com/lowagie/text/pdf/fonts/*.afm (DroidText) is bundled with your APK. Please examine your APK, you'll see that they're missing! This isn't an iText question, this is a question about how to bundle files that aren't class files with your APK. – Bruno Lowagie Aug 08 '13 at 15:34
  • solve it, mainly because the way to import the library :)) Thx Lowagie – codermomo Aug 08 '13 at 16:05