How to create PDFs in an Android app? [closed] How to create PDFs in an Android app? [closed] android android

How to create PDFs in an Android app? [closed]


If anyone wants to generate PDFs on Android device, here is how to do it:


If you are developing for devices with API level 19 or higher you can use the built in PrintedPdfDocument: http://developer.android.com/reference/android/print/pdf/PrintedPdfDocument.html

// open a new documentPrintedPdfDocument document = new PrintedPdfDocument(context,     printAttributes);// start a pagePage page = document.startPage(0);// draw something on the pageView content = getContentView();content.draw(page.getCanvas());// finish the pagedocument.finishPage(page);. . .// add more pages. . .// write the document contentdocument.writeTo(getOutputStream());//close the documentdocument.close();


A trick to make a PDF with complex features is to make a dummy activity with the desired xml layout. You can then open this dummy activity, take a screenshot programmatically and convert that image to pdf using this library. Of course there are limitations such as not being able to scroll, not more than one page,but for a limited application this is quick and easy. Hope this helps someone!