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.

Vishwas Rao
  • 121
  • 1
  • 1
  • 11

2 Answers2

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.
Islam Salah
  • 953
  • 11
  • 22
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!!!

Amit Goyal
  • 56
  • 2
  • 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. – Vishwas Rao Oct 15 '16 at 08: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) – Vishwas Rao Oct 15 '16 at 08:50
  • For examples see https://developer.android.com/training/printing/html-docs – Evgenii Vorobei Sep 03 '20 at 08:43