Convert HTML to PDF in Android

HTML to PDF conversion is available for Android API 19 and above.

Internet connection is not required when converting local HTML files. However, it is required for converting HTTP/HTTPS links.

The HTML to PDF conversion API accepts different sources, such as HTTP/HTTPS URLs, HTML strings, and everything that can be loaded in a WebView.

To convert a link URL to PDF, simply:

1public void convertFromUrl(Context context, String url, Uri folder, String outputFileName) {
2 HTML2PDF.fromUrl(context, url, folder, outputFileName, new HTML2PDF.HTML2PDFListener() {
3 @Override
4 public void onConversionFinished(String pdfOutput, boolean isLocal) {
5 // Handle callback when conversion finished
6 }
7
8 @Override
9 public void onConversionFailed(String error) {
10 // Handle callback if conversion failed
11 }
12 });
13}

From HTML string

To convert an HTML string to PDF, simply:

1public void convertFromHtml(Context context, String baseUrl, String htmlSting) {
2 HTML2PDF.fromHTMLDocument(context, baseUrl, htmlSting, new HTML2PDF.HTML2PDFListener() {
3 @Override
4 public void onConversionFinished(String pdfOutput, boolean isLocal) {
5 // Handle callback when conversion finished
6 }
7
8 @Override
9 public void onConversionFailed(String error) {
10 // Handle callback if conversion failed
11 }
12 });
13}

where myBaseUrl is the URL to use as the page's base URL. If null defaults to about:blank and myHtmlData is a String of data in the UTF-8 encoding. Click here for more info.

From other sources

If none of the above fit your needs, you can also pass in a WebView directly for conversion:

1public void convertFromWebView(WebView myWebview) {
2 HTML2PDF.fromWebView(myWebview, new HTML2PDF.HTML2PDFListener() {
3 @Override
4 public void onConversionFinished(String pdfOutput, boolean isLocal) {
5 // do something with the PDF output
6 }
7
8 @Override
9 public void onConversionFailed(String error) {
10 // handle error
11 }
12 });
13}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales