Learn to generate PDF documents for your Android Application

Generate a PDF document is a well mastered task by Java developers on desktop application or on web applications. On Android applications, it’s a little bit different and only a few developers add that kind of feature in their app. However, users really enjoy having some reports for monitoring applications for example. A solution can be to use iText library in your application. iText is a library created for Java applications and can be added as a Jar dependency to your Android project. Main issue is the size of the library. On Android applications, each byte is counted and so, a lightweight solution must be preferred. A lightweight and still powerful solution can be to use the Android PDF Writer (APW) implementation that is offered under BSD license and is available here : http://coderesearchlabs.com/androidpdfwriter .

Easiest solution to use APW Library in your Android project is to download the library and add the project in your build.gradle file as a dependency like that :


compile project(':apwlibrary')

Then, you have to create an instance of PDFWriter object with a given size defined thanks to constants in APW Library :


PDFWriter writer = new PDFWriter(PaperSize.FOLIO_WIDTH, PaperSize.FOLIO_HEIGHT);

You can now define a specific Font for your PDF :


writer.setFont(StandardFonts.SUBTYPE, StandardFonts.TIMES_BOLD, StandardFonts.WIN_ANSI_ENCODING);

And add some text :


writer.addText(x, y, size, "I like SSaurel’s Tutorials !");

Obviously, APW Library lets you to add images on your PDF :


Bitmap logo = BitmapFactory.decodeStream(assetManager.open("cpuinfo_logo.png"));
Bitmap qrcode = BitmapFactory.decodeStream(assetManager.open("qrcode.png"));

// center images ...
int left = (PaperSize.FOLIO_WIDTH - logo.getWidth()) / 2;
writer.addImage(left, PaperSize.FOLIO_HEIGHT - 490, logo);
left = (PaperSize.FOLIO_WIDTH - qrcode.getWidth()) / 2;
writer.addImage(left, PaperSize.FOLIO_HEIGHT - 720, qrcode);

May be now, you wanna star a new page for your PDF report ? Simply use the newPage() dedicated method :


writer.newPage();

Note that you can change font when you want in your PDF. APW Library has a lot more options. For example, you can rotate your texts or add some shapes like a rectangle.

Last step is to generate your PDF report. For that, you must get your PDF content as a string thanks to method asString() from PDFWriter instance object. Then, create a file on Android with that content as Bytes :


public void outputToFile(String fileName, String pdfContent, String encoding) {
 File newFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);

 try {
  newFile.createNewFile();

  try {
    FileOutputStream pdfFile = new FileOutputStream(newFile);
    pdfFile.write(pdfContent.getBytes(encoding));
    pdfFile.close();
  } catch(FileNotFoundException e) {
   // ...
  }

 } catch(IOException e) {
  // ...
 }
}

Then, call the method :


outputToFile("MyFirstReport.pdf", writer.asString(), "ISO-8859-1");

Now, we can show the final result with our generated report :

figure_pdf_report

 

figure_pdf_report2

 

Like you can see, APW Library is a lightweight and powerful solution to generate quickly simple PDF documents on Android. For more complex PDF documents, a better approach will be to use iText. Heavier but surely more complete.

Leave a Reply