manivel ramana
manivel ramana

Reputation: 61

how to download HTML page as pdf in Android?

I am new to Android Development. I created a app using only html, css and jquery. when we click a button a particular DOM element should download as pdf.

To download a DOM Element as pdf I have used JSPDF. Its working properly in chrome. The same is not working in android app.

Then after some browsing I found JSPDF won't work in android app. So to download pdf I have used @JavascriptInterface and DownloadManager.

Its working properly when I give a proper url with HTTP but If i send the URL from JS to java to download pdf Its not working.

What changes I have to make to download the html page as pdf.

Thanks in advance :)

JS Code to Call java function when button is clicked

                $(".buttonClick").click(function () {
                    $(".mainPage").hide();
                    $(".bill").show();

                    var doc = new jsPDF();
                    doc.addHTML(document.body, function () {
                        var pdfOutput = doc.output("blob");
                       // window.open(URL.createObjectURL(pdfOutput));
                        Android.downloadPDF(URL.createObjectURL(pdfOutput));
                    });

                });

And the Java code is given below:

public class CustomFunctions {

    @JavascriptInterface
    public void downloadPDF(String url) {
        final Context c = HelperClass.getContext();
        //WebView mWebView=new WebView(HelperClass.getContext());
        WebView mWebView = HelperClass.getWebView();
        //String userAgent=new WebView(HelperClass.getContext()).getSettings().getUserAgentString();
        //String url = "http://www.africau.edu/images/default/sample.pdf";

        try {
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "myPDFfile.pdf");
            DownloadManager dm = (DownloadManager) c.getSystemService(c.DOWNLOAD_SERVICE);
            dm.enqueue(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 3

Views: 1909

Answers (1)

Naveed Abbas
Naveed Abbas

Reputation: 218

This library uses a binary of wkhtmltopdf, Apache Exec, and synchronization for a robust, thread-safe HTML to PDF conversion process.

It includes the binaries for Windows, Mac & Linux and will decide which to use based on the current operating system. Optionally you can provide your own wkhtmltopdf binary by setting the WKHTMLTOPDF_BINARY environment variable.

Limitations This library aims at simplifying the usage of wkhtmltopdf in Java, which means this library is limited to only what that tool can do and also shares any bugs that tool might contain.

This library is written and compiled with Java 8. Compile from source if you need to target a lower Java version.

Adding this to your project n your build.gradle file:

Under repositories Add

maven { url "https://jitpack.io" }

, making sure it's the last repo declared Under dependencies

Add compile 'com.github.todd-elvers:html-to-pdf-converter:2.0.0'

In the following example we write the resulting PDF to a temporary directory, then write it to some stream, and then the try-with-resources block cleans up the PDF from disk

HtmlToPdfFileConverter htmlToPdfFileConverter = new HtmlToPdfFileConverter();

try (PdfFile pdfFile = htmlToPdfFileConverter.tryToConvert(html)) {
    pdfFile.writeToOutputStream(...);
}

If you want to avoid the intermediate step of writing to a temporary directory and instead write directly to the destination:

HtmlToPdfFileConverter htmlToPdfFileConverter = new HtmlToPdfFileConverter();
File outputFile = ...

try {
    htmlToPdfFileConverter.tryToConvert(html, outputFile);
} catch(HtmlToPdfConversionException ex) {
    ...

Upvotes: 2

Related Questions