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
- Convert HTML to PDF
- Convert HTML to Password-protected PDF
- HTML to PDF Conversion using URL
- Get a Free Licence
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.
- Create an object of the HtmlLoadOptions class.
- Create an object of Document class and initialize it with the HTML file’s path and HtmlLoadOptions object.
- Save HTML as PDF using Document.save(String) method.
The following code sample shows how to convert an HTML file to PDF.
HTML File
HTML to PDF
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.
- Create an object of the HtmlLoadOptions class.
- Create an object of the Document class and initialize it with the HTML file’s path and HtmlLoadOptions object.
- Use Document.encrypt(String, String, Permissions, CryptoAlgorithm) method to password-protect the PDF.
- Save HTML as PDF using Document.save(String) method.
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 InputStream object.
- 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.