2

Is it possible to convert html to pdf using jsPdf in android app ( without using cordova or phone gap) and save the file in device local.?

I tried this and onclick of button i am calling jspdf convertion logic, but pdf is not generating.

In adb log I see below waring on click of button.

Cannot call determinedVisibility() - never saw a connection for the pid: 1153

I am using jspdf library, Filesaver.js.

3
  • have you found a solution? care to share?
    – beerBear
    Commented Oct 26, 2016 at 7:08
  • No, I am still trying to find solution. If I try to load the HTML file in android chrome browser jsPDF is converting and saving pdf file. But it is failing in app ( chromium webview is being used) Commented Oct 26, 2016 at 8:17

2 Answers 2

9

This class might help you. But to compile, it must be in package ".../java/android/print/"

Here is a simple code example :

PdfConverter converter = PdfConverter.getInstance();
File file = new File(Environment.getExternalStorageDirectory().toString(), "file.pdf");
String htmlString = "<html><body><p>WHITE (default)</p></body></html>";
converter.convert(getContext(), htmlString, file);
// By now the pdf has been printed in the file.
7
  • You completely replaced the Android "copy left" AGPL dependency? That is genius! Thank you! Commented Feb 15, 2018 at 0:08
  • 1
    @EdoftheMountain Thank you! Commented Feb 16, 2018 at 6:56
  • 'LayoutResultCallback()' is not public in 'android.print.PrintDocumentAdapter.LayoutResultCallback'. Cannot be accessed from outside package
    – behelit
    Commented Mar 21, 2019 at 10:41
  • 1
    Have you placed the class, shared in the answer, in a package with a hierarchy like ".../java/android/print/" ? Commented Mar 22, 2019 at 12:12
  • @IslamSalah How can we place the class in in a package with a hierarchy like the above? Commented Mar 11, 2020 at 14:32
1

If you are developing for devices with API level 19+, then you can avoid 3rd party library. Please check the following link:

http://developer.android.com/reference/android/print/pdf/PrintedPdfDocument.html

If you are developing for API level below 19, then check the following links:

http://manzzup.blogspot.in/2014/05/creating-pdf-file-from-dynamic-html-in.html

If you strictly want to use jsPdf, then try to use jspdf.debug.js.

Cheers!!!

3
  • Currently I am using androids Print framework. We want pdf to be generated and saved without any popup. That is not supported in android print framework. Commented Oct 15, 2016 at 8:46
  • I have included jspdf library, jspdf.debug.js. I have a button on HTML page to call pdf conversion, which calls jspdf function. But onclik of button not generating pdf . If I open the HTML file on browser in android device, it tries to generate and download the pdf file ( download blocked saying can download only from HTTP or HTTPS URLs- this is other issue) Commented Oct 15, 2016 at 8:50
  • For examples see developer.android.com/training/printing/html-docs Commented Sep 3, 2020 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.