Converting HTML to PDFkeyboard_arrow_down

Converting HTML to PDF

In this document
chevron_rightFrom link
chevron_rightFrom HTML string
chevron_rightFrom other sources
HTML to PDF conversion is available for Android API 21 and above.

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.

linkFrom link

To convert a link URL to PDF, simply:

HTML2PDF.fromUrl(context, "http://developer.android.com/about/index.html", new HTML2PDF.HTML2PDFListener() {
    @Override
    public void onConversionFinished(String pdfOutput) {
        // do something with the PDF output
    }

    @Override
    public void onConversionFailed() {
        // handle error
    }
});

linkFrom HTML string

To convert an HTML string to PDF, simply:

HTML2PDF.fromHTMLDocument(activity, myBaseUrl, myHtmlData, new HTML2PDF.HTML2PDFListener() {
    @Override
    public void onConversionFinished(String pdfOutput) {
        // do something with the PDF output
    }

    @Override
    public void onConversionFailed() {
        // handle error
    }
});

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.

linkFrom other sources

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

HTML2PDF.fromWebView(myWebview, new HTML2PDF.HTML2PDFListener() {
    @Override
    public void onConversionFinished(String pdfOutput) {
        // do something with the PDF output
    }

    @Override
    public void onConversionFailed() {
        // handle error
    }
});