androidandroid-print-frameworkandroid-pdf-api

What is the best strategy for creating, displaying and printing pdf documents in android


I plan to use the built-in android pdf library. In my app I need to display a pdf report that may consist of several pages. Users should be able to print those pages. Printing with the PrintDocumentAdapter seems pretty straight forward, but what's unclear to me is what is the best way to create the pdf. I know that you can generate a PdfDocument with simply a View/canvas and or take more "low-level" approach where you draw lines,text, paint, etc.

I see three possibilities:

  1. Create a view for each page. The user can navigate between the views as needed and print. However it's unclear to me how to generate a pdf for each page/view. What I mean by this is if I'm viewing page/view 1, yes I can easily create a pdf from this, but what about the other pages? Yes I can have this in memory, but what I've found is if they aren't actively being displayed on screen, they create empty pdfs. I don't want to have the user print each page individually.

  2. Create the pdf documents (low-level approach), integrate a pdf reader and just display/print the pdf from there.

  3. Create a view for each page that the user can navigate through. When the print option is invoked, generate the pdf documents again (low-level approach)

Obviously option 1 is the preferred approach, but I'm not clear on how I can do that. Of course, I could be missing something here so any help would be appreciated!


Solution

  • ...but what's unclear to me is what is the best way to create the pdf...

    Yet, it is relative. It really is a matter of what library/framework you find more convenient to work with, because each of them offers quit the same functionality with its own disadvantages. For instance, those I worked with, have the following ones:

    1. iText - you must buy a licenece for comircial use (see What is latest version of itext that is not AGPL?);
    2. Android Print Framework - no built-in pdf preview before printing as you mentioned in the 2nd item (you can use built-in PdfRenderer though for API 21+; also see Display PDF within app on Android?). Another disadvantage is the disturbing system dialogs when you'd like to save/print "silently" :)

    Even the official documentation states that "You can use any PDF generation library to generate a PDF document and pass it to the Android print framework for printing."

    ...Yes I can have this in memory, but what I've found is if they aren't actively being displayed on screen, they create empty pdfs...

    The reason for empty pdfs when you keep the views that have been left by a user in memory is their 0-dimension because of not being laied out (see Android: PdfDocument generates empty pdf). The same will happen if you try to inflate the views that haven't been reached by a user yet... In fact, there is nothing you can do about it, this is how Android works.

    I don't want to have the user print each page individually.

    In this case I suggest you to create pdf pages in advance for each content view a user is currently interacting with, just make sure that the view is still laied out when drawing it on canvas. For the views that aren't currently laied out use the "low-level approach". And once printing requested pass the output pdf to Android Print Framework.