11package  com .pdf .generator ;
22
3- import  android .content .ActivityNotFoundException ;
43import  android .content .Context ;
5- import  android .content .Intent ;
6- import  android .graphics .Canvas ;
7- import  android .graphics .Rect ;
8- import  android .graphics .pdf .PdfDocument ;
9- import  android .net .Uri ;
10- import  android .os .ParcelFileDescriptor ;
114import  android .print .PrintManager ;
125import  android .util .Log ;
13- import  android .view .View ;
146import  android .webkit .WebView ;
157
16- import  org .apache .cordova .CordovaInterface ;
17- import  org .apache .cordova .CordovaPlugin ;
188import  org .apache .cordova .CallbackContext ;
19- 
9+ import   org . apache . cordova . CordovaPlugin ; 
2010import  org .apache .cordova .LOG ;
2111import  org .json .JSONArray ;
2212import  org .json .JSONException ;
23- import  org .json .JSONObject ;
24- 
25- import  java .io .File ;
26- import  java .io .FileNotFoundException ;
27- import  java .io .FileOutputStream ;
28- import  java .io .IOException ;
29- import  java .io .OutputStream ;
3013
3114/** 
32- * This class echoes  a string called  from JavaScript.  
33- */ 
15+   * This plugin creates  a PDF  from a given HTML website or string 
16+   */
3417public  class  PDFGenerator  extends  CordovaPlugin  {
35- 
36-     private  final  static  String  APPNAME  = "PDFGenerator" ;
37-     private  WebView  offscreenWebview  = null ;
18+     private  final  static  String  LOG_TAG  = "PDFGenerator" ;
3819
3920    public  WebView  getOffscreenWebkitInstance (Context  ctx ) {
40-         LOG .i (APPNAME , "Mounting offscreen webview" );
41-         if  (this .offscreenWebview  == null ){
42-             WebView  view  = new  WebView (ctx );
43-             view .getSettings ().setDatabaseEnabled (true );
44-             view .getSettings ().setJavaScriptEnabled (true );
45- 
46-             return  this .offscreenWebview  = view ;
47-         }else {
48-             return  this .offscreenWebview ;
49-         }
21+         LOG .d (LOG_TAG , "Mounting offscreen WebView" );
22+         WebView  view  = new  WebView (ctx );
23+         view .getSettings ().setDatabaseEnabled (true );
24+         view .getSettings ().setJavaScriptEnabled (true );
25+         return  view ;
5026    }
5127
5228    @ Override 
53-     public  boolean  execute (String  action , JSONArray  args , CallbackContext  callbackContext ) throws   JSONException   {
54-         if  (action . equals ( "htmlToPDF" )) {
29+     public  boolean  execute (String  action , JSONArray  args , CallbackContext  callbackContext ) {
30+         if  ("htmlToPDF" . equals ( action )) {
5531            this .pdfPrinter (args , callbackContext );
56- 
5732            return  true ;
5833        }
5934        return  false ;
@@ -64,52 +39,45 @@ public void onResume(boolean multitasking) {
6439        super .onResume (multitasking );
6540    }
6641
67-     private  void  pdfPrinter (final  JSONArray  args , final  CallbackContext  callbackContext ) throws   JSONException   {
42+     private  void  pdfPrinter (final  JSONArray  args , final  CallbackContext  callbackContext ) {
6843        final  Context  ctx  = this .cordova .getActivity ().getApplicationContext ();
69-         final  CordovaInterface  _cordova  = this .cordova ;
70-         final  CallbackContext  cordovaCallback  = callbackContext ;
7144
72-         _cordova .getActivity ().runOnUiThread (new  Runnable () {
73-             @ Override 
74-             public  void  run () {
75-                 try  {
76-                     WebView  webview  = getOffscreenWebkitInstance (ctx );
45+         cordova .getActivity ().runOnUiThread (() -> {
46+             try  {
47+                 WebView  webview  = getOffscreenWebkitInstance (ctx );
7748
78-                      PrintManager  printManager  = (PrintManager ) cordova .getActivity ()
79-                              .getSystemService (Context .PRINT_SERVICE );
49+                 PrintManager  printManager  = (PrintManager ) cordova .getActivity ()
50+                         .getSystemService (Context .PRINT_SERVICE );
8051
81-                      boolean  outputBase64  = args .getString (4 ) != null  && args .getString (4 ).equals ("base64" );
82-                      PDFPrinterWebView  printerWebView  = new  PDFPrinterWebView (printManager , ctx , outputBase64 );
52+                 boolean  outputBase64  = args .getString (4 ) != null  && args .getString (4 ).equals ("base64" );
53+                 PDFPrinterWebView  printerWebView  = new  PDFPrinterWebView (printManager , ctx , outputBase64 );
8354
84-                      String  fileNameArg  = args .getString (5 );
85-                      if  (fileNameArg  != null ) {
86-                          printerWebView .setFileName (fileNameArg );
87-                      }
55+                 String  fileNameArg  = args .getString (5 );
56+                 if  (fileNameArg  != null ) {
57+                     printerWebView .setFileName (fileNameArg );
58+                 }
8859
89-                      String  pageType  = args .getString (2 );
90-                      printerWebView .setPageType (pageType );
60+                 String  pageType  = args .getString (2 );
61+                 printerWebView .setPageType (pageType );
9162
92-                      String  orientation  = args .getString (3 );
93-                      if  (orientation  != null ) {
94-                          printerWebView .setOrientation (orientation );
95-                      }
63+                 String  orientation  = args .getString (3 );
64+                 if  (orientation  != null ) {
65+                     printerWebView .setOrientation (orientation );
66+                 }
9667
97-                      printerWebView .setCordovaCallback (cordovaCallback );
98-                      webview .setWebViewClient (printerWebView );
68+                 printerWebView .setCordovaCallback (callbackContext );
69+                 webview .setWebViewClient (printerWebView );
9970
100-                      if  (args .getString (0 ) != null  && !args .getString (0 ).equals ("null" ))
101-                          webview .loadUrl (args .getString (0 ));
71+                 if  (args .getString (0 ) != null  && !args .getString (0 ).equals ("null" ))
72+                     webview .loadUrl (args .getString (0 ));
10273
103-                      if  (args .getString (1 ) != null  && !args .getString (1 ).equals ("null" ))
104-                          webview .loadDataWithBaseURL (null ,args .getString (1 ), "text/HTML" ,"UTF-8" , null );
74+                 if  (args .getString (1 ) != null  && !args .getString (1 ).equals ("null" ))
75+                     webview .loadDataWithBaseURL (null ,  args .getString (1 ), "text/HTML" ,  "UTF-8" , null );
10576
106-                 } catch  (JSONException  e ) {
107-                     e .printStackTrace ();
108-                     Log .e (APPNAME , e .getMessage ());
109-                     cordovaCallback .error ("Native pasing arguments: "  + e .getMessage ());
110-                 }
77+             } catch  (JSONException  e ) {
78+                 Log .e (LOG_TAG , "Unable to parse JSON" , e );
79+                 callbackContext .error ("Native parsing arguments: "  + e .getMessage ());
11180            }
11281        });
11382    }
114- 
11583}
0 commit comments