Skip to content

Commit d927e2d

Browse files
committed
fix(android): fix crashes and faulty preview on Chrome 86+
This is achieved by always using a new WebView instance and by destorying the old one fix(android): correct file name for temp folder fix(android): remove unused config file fix(android): register only one printer adapter fix(android): do not crash if error code is null (which is valid) refactor(android): use Cordova's log helper style: reformat codebase
1 parent 5eb764e commit d927e2d

File tree

7 files changed

+171
-285
lines changed

7 files changed

+171
-285
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pdf.fromData( '<html><h1>Hello World</h1></html>', options)
172172

173173

174174

175-
##### filename
175+
##### fileName
176176

177177
- You can specify the name of the PDF file.
178178

src/android/PDFConfig.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/android/PDFGenerator.java

Lines changed: 38 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,34 @@
11
package com.pdf.generator;
22

3-
import android.content.ActivityNotFoundException;
43
import 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;
114
import android.print.PrintManager;
125
import android.util.Log;
13-
import android.view.View;
146
import android.webkit.WebView;
157

16-
import org.apache.cordova.CordovaInterface;
17-
import org.apache.cordova.CordovaPlugin;
188
import org.apache.cordova.CallbackContext;
19-
9+
import org.apache.cordova.CordovaPlugin;
2010
import org.apache.cordova.LOG;
2111
import org.json.JSONArray;
2212
import 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+
*/
3417
public 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
}

src/android/PDFPrinter.java

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package com.pdf.generator;
22

3-
import android.print.PDFConfig;
43
import android.os.Build;
54
import android.os.Bundle;
65
import android.os.CancellationSignal;
76
import android.os.ParcelFileDescriptor;
87
import android.print.PageRange;
98
import android.print.PrintAttributes;
109
import android.print.PrintDocumentAdapter;
11-
import android.print.PrintDocumentInfo;
1210
import android.webkit.WebView;
1311

1412
import org.apache.cordova.LOG;
@@ -18,53 +16,47 @@
1816
*/
1917

2018
public class PDFPrinter extends PrintDocumentAdapter {
21-
22-
static final String APPNAME = "PDFPrinter";
23-
24-
private PrintDocumentAdapter mWrappedInstance = null;
25-
private WebView webView = null;
26-
private PrintAttributes attributes = null;
27-
private PDFConfig config = null;
19+
private static final String LOG_TAG = "PDFPrinter";
20+
private final WebView webView;
21+
private PrintDocumentAdapter mWrappedInstance;
2822

2923
public PDFPrinter(WebView webView, String fileName) {
30-
if (Build.VERSION.SDK_INT >= 21) {
24+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
3125
this.mWrappedInstance = webView.createPrintDocumentAdapter(fileName);
3226
} else {
3327
this.mWrappedInstance = webView.createPrintDocumentAdapter();
3428
}
35-
config = new PDFConfig();
3629
this.webView = webView;
3730
}
3831

39-
40-
4132
@Override
4233
public void onStart() {
4334
mWrappedInstance.onStart();
4435
}
4536

4637
@Override
4738
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
48-
CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
49-
39+
CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
40+
5041
mWrappedInstance.onLayout(
51-
oldAttributes,
52-
newAttributes,
53-
cancellationSignal,
54-
callback,
55-
extras
42+
oldAttributes,
43+
newAttributes,
44+
cancellationSignal,
45+
callback,
46+
extras
5647
);
5748
}
5849

5950
@Override
6051
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal,
61-
WriteResultCallback callback) {
52+
WriteResultCallback callback) {
6253
mWrappedInstance.onWrite(pages, destination, cancellationSignal, callback);
6354
}
6455

6556
@Override
6657
public void onFinish() {
67-
LOG.i(APPNAME, "Cleaning pdfwriter & webView objects.");
58+
LOG.d(LOG_TAG, "Cleaning pdfwriter & webView objects.");
6859
mWrappedInstance.onFinish();
60+
webView.destroy();
6961
}
7062
}

0 commit comments

Comments
 (0)