Generating PDFs Dynamically on Android

Fri, June 15 2012, 11:35 AM (US Eastern Time)

I have been using an Android phone for a few years now, and one day I decided I should try to write an Android application using our DynamicPDF Generator for Java. So I decided to start off with a simple "Hello World" application based on the DynamicPDF Generator hello world PDF java example provided with the Generator product.

My goal was to create a PDF on the Android phone when the application was run. I wanted to keep it simple and not even bother with having a GUI for it at this point.

I already had my machine set up with Eclipse IDE, the ADT (Android Developers Tool) plugin and the Android SDK. Here are the steps I followed to create the Android application:

  1. I created a new Android project in Eclipse with the details below. Android version 2.1 is what I tested with, since that is the lowest SDK version I had installed, but it may work with an older version of Android too.

    Project name: HelloWorld
    Build Target: Android 2.1
    Application name: Hello World
    Package name: com.cete.androidexamples.dynamicpdf.helloworld
    Create Activity: DynamicPDFHelloWorld
    Minimum SDK: 7

  2. Since I knew my application had to write to the SD card to output the PDF, I added the WRITE_EXTERNAL_STORAGE permission in the AndroidManifest.xml:
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  3. My application uses the DynamicPDF.jar file, so I created a libs folder under the project, copied the DynamicPDF.jar file to it and added that to the build path. (A free evaluation version of DynamicPDF.jar can be downloaded here: DynamicPDF Generator for Java).

  4. Now it was time to add the code to generate the PDF document. So I edited the source file DynamicPDFHelloWorld.java and here is what it looked like when I was done:
    package com.cete.androidexamples.dynamicpdf.helloworld;
    
    import com.cete.dynamicpdf.*;
    import com.cete.dynamicpdf.pageelements.Label;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.widget.Toast;
    
    public class DynamicPDFHelloWorld extends Activity {
    	private static String FILE = Environment.getExternalStorageDirectory()
    			+ "/HelloWorld.pdf";
    
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    
    		// Create a document and set it's properties
    		Document objDocument = new Document();
    		objDocument.setCreator("DynamicPDFHelloWorld.java");
    		objDocument.setAuthor("Your Name");
    		objDocument.setTitle("Hello World");
    
    		// Create a page to add to the document
    		Page objPage = new Page(PageSize.LETTER, PageOrientation.PORTRAIT,
    				54.0f);
    
    		// Create a Label to add to the page
    		String strText = "Hello World...\nFrom DynamicPDF Generator "
    				+ "for Java\nDynamicPDF.com";
    		Label objLabel = new Label(strText, 0, 0, 504, 100,
    				Font.getHelvetica(), 18, TextAlign.CENTER);
    
    		// Add label to page
    		objPage.getElements().add(objLabel);
    
    		// Add page to document
    		objDocument.getPages().add(objPage);
    
    		try {
    			// Outputs the document to file
    			objDocument.draw(FILE);
    			Toast.makeText(this, "File has been written to :" + FILE,
    					Toast.LENGTH_LONG).show();
    		} catch (Exception e) {
    			Toast.makeText(this,
    					"Error, unable to write to file\n" + e.getMessage(),
    					Toast.LENGTH_LONG).show();
    		}
    	}
    }

    If you are familiar with the DynamicPDF Generator for Java "Hello World" example, you will see most of the code is copied from there. The main thing to note is that the way the file path is specified is different if you want to write to the SD card, and you will be familiar with this if you are familiar with Android development.

  5. Now it was time to run the application and see if it works. I had already created an Android Virtual Device with a SD card and target of Android 2.1 so when I ran it, the application was installed and launched, and the toast message indicated that the PDF file had been created. I checked the root of the SD card folder for "HelloWorld.pdf" and there it was!

If you check the LogCat, you may notice that there are warnings saying the Dalvik VM is unable to find the HttpServletRequest and HttpServletRequest response classes. Since the DynamicPDF.jar file is not built specifically for Android and has references to some Java classes which are not supported by the Dalvik VM, this is expected.

Now that I got the basic example working, my next target was to see if the "AllPageElements" example would work. I will write more about that in another blog post.

If you are interested in trying out this sample, you can download the sample Eclipse project with the source code. Please make sure you include a copy of the latest version of the DynamicPDF.jar file under the libs folder.

Have you tried using DynamicPDF on Android or do you have a need to create PDFs at run-time in your Android applications? I would love to hear your comments.

Downloads:

HelloWorld.zip (39 kb)

Tags: , , , ,

Comments (2) -

Yakhtar Ali
3/10/2014 2:20:04 AM #

pretty poor example... thanks for wasting my time  

Ramesh
3/21/2014 2:16:25 AM #

really good example. i love it.
  If its possible please send example that add an image into the pdf file. tahnks in advance

Add comment

biuquote
  • Comment
  • Preview
Loading

Month List