HTML to PDF Android Java

In various cases, you may need to convert the HTML pages to PDF format. On the other hand, you may want to generate PDF from the content of a WYSIWYG HTML editor. For such scenarios, this article covers how to convert HTML to PDF in Android apps programmatically. Furthermore, you will learn about additional options to customize HTML to PDF conversion.

Android HTML to PDF Converter Library

In order to convert HTML to PDF, we’ll use Aspose.PDF for Android via Java library. It provides a complete set of features to create, modify or convert PDF files programmatically from within your Android apps. You can either download the library files or install them using the following configurations.

maven {
    url "http://repository.aspose.com/repo/" }
compile (
        group: 'com.aspose',
        name: 'aspose-pdf',
        version: '20.11',
        classifier: 'android.via.java')

Convert HTML to PDF in Android Programmatically

The following are the steps to convert HTML to PDF in Android programmatically.

The following code sample shows how to convert an HTML file to PDF.

HTML File

HTML to PDF Android

HTML to PDF

Android HTML to PDF Converter Library

Convert HTML to Password-protected PDF

You can also convert HTML to a password-protect PDF within a few lines of code. The following are the steps to perform this operation.

The following code sample shows how to convert HTML to password-protected PDF.

HTML to PDF Conversion using URL

The following are the steps to convert a web page to PDF using a URL.

  • Create and initialize the URL object.
  • Get the web page into the InputStreamobject.
  • Create the Document object and initialize it with _InputStream _and _HtmlLoadOptions _objects.
  • Save HTML as PDF using Document.save(String) method.

The following code sample shows how to save a web page as PDF in Android programmatically.

Get a Free License

You can get a free license in order to use Android HTML to PDF converter library without evaluation limitations.

Conclusion

In this article, you have learned how to convert HTML files to PDF in Android programmatically. Furthermore, you have seen how to convert a web page to PDF or generate a password-protected PDF from HTML. You can explore more about the Android PDF library using documentation. In case you would have any queries, feel free to let us know via our forum.

See Also