Skip to content

Commit fb26ba6

Browse files
fix(webview): android print feature added
1 parent 5c33883 commit fb26ba6

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
import java.util.List;
4141
import java.util.Map;
4242

43+
import android.print.PrintAttributes;
44+
import android.print.PrintManager;
45+
4346
public class RNCWebView extends WebView implements LifecycleEventListener {
4447
protected @Nullable
4548
String injectedJS;
@@ -292,9 +295,9 @@ public void callInjectedJavaScript() {
292295
public void callInjectedJavaScriptBeforeContentLoaded() {
293296
if (getSettings().getJavaScriptEnabled()) {
294297
// This is necessary to support window.print() in the loaded web pages.
295-
evaluateJavascriptWithFallback(
296-
"(function(){window.print=function(){window.ReactNativeWebView.print();};})();"
297-
);
298+
// evaluateJavascriptWithFallback(
299+
// "(function(){window.print=function(){window.ReactNativeWebView.print();};})();"
300+
// );
298301

299302
if (injectedJSBeforeContentLoaded != null &&
300303
!TextUtils.isEmpty(injectedJSBeforeContentLoaded)) {
@@ -330,6 +333,14 @@ public void run() {
330333
} else {
331334
dispatchEvent(webView, new TopMessageEvent(RNCWebViewWrapper.getReactTagFromWebView(webView), data));
332335
}
336+
337+
if(message.equals("android-print")){
338+
android.print.PrintDocumentAdapter printAdapter = mWebView.createPrintDocumentAdapter("Form");
339+
PrintManager printManager = (PrintManager) reactContext.getSystemService(Context.PRINT_SERVICE);
340+
if (printManager != null) {
341+
printManager.print("Form", printAdapter, new PrintAttributes.Builder().build());
342+
}
343+
}
333344
}
334345
});
335346
} else {

0 commit comments

Comments
 (0)