Android Question Webview printing from smart form

Setlodi

Member
Hi There

I'm loading a Jotform smart form in webview. My form has a print button that prints formatted content of the form. So, when I click print on this form, it doesn't do anything. I have run the same smart form in Google Chrome and I'm able to print. I have no idea what I must do to be able to print within webview.

This is how I'm loading my form:
Sub Activity_Create(FirstTime As Boolean)
    jo.InitializeContext
    webview.Initialize("webview")
    Activity.AddView(webview, 0%x,0%y,100%x,100%y)
    jo.runmethod("addChromeClient", Array(webview))
    webpage = "https://form.jotform.com/jsetlodi/print"
    webview.LoadUrl(webpage)
End Sub

Any ideas?
Thanks in advance.
Setlodi
 

drgottjr

Expert
Licensed User
Longtime User
1) line #5 has to go. unless you intend to implement chromeclient yourself.
2) you need to intercept the print button and divert it to your own print routine.
3) you'll need to use a library like webviewextras to set up an active chromeclient and a javascript interface.

this snippet (tested by me) will intercept the print button:

JavaScript:
Dim js As String = $"javascript:(function(){
      var e = document.getElementById('input_print_255');
      e.addEventListener('click', function(e) {
        console.log("you clicked print");
          alert("you clicked on print");
           // IF YOU CAN EXTRACT THE JOB'S KEY
           // YOU CAN PROBABLY DOWNLOAD THE
          // FORMATTED OUTPUT AND PRINT IT YOURSELF
      });
    })();"$


    wvx.executeJavascript(webview,js)    ' this line is b4a code, not javascript

the button interception and print routine themselves are not difficult.
what will be tricky is how you acquire the formatted output.
i have no idea what jotform is/does or exactly what you're supposed
to do with it, but it appears to handle formatting itself.

web printing is designed to print a rendered webpage. this is what
jotform hands to your browser in the form of your formatted text. your
browser effectively "clicks" the print button and you see the familiar
print dialog. this doesn't work on webview, which is why nothing happens.
you need to create the print job yourself, but you need the
formatted output. jotform grabs it from the mother ship; in
theory you could do the same, but you need the job number.
and some patience. and some expertise. how much time are you
willing to spend on this? webview is not a browser.
 
Upvote 0

Setlodi

Member
I am doing a workaround because in reality, I want to print the PDF that is generated from submitting the form. I can get Jotform to save the PDF file in a Google drive folder. Now the issue becomes, how do I open a pdf file stored in a Google drive folder? I'm aware that you cannot preview PDF in webview.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
open it? why? just download it with okhttputils2 and do what you want with it once you have it.
 
Upvote 0

Setlodi

Member
open it? why? just download it with okhttputils2 and do what you want with it once you have it.
There's going to be a lot of files and I just want to print not save. The files are saved in Google drive. This app is for issuing citations and after a user has submitted information, they must just view to make sure that correct info was captured then print.
 
Upvote 0

Ivica Golubovic

Active Member
Licensed User
  1. Ok, download WebKitLibrarySet from this forum.
  2. Extract zip file to additional library folder.
  3. For your project use WebkitWebViewClient and WebkitWebResourceRequest from WebkitLibrarySet.
Example:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private WebView1 As WebView
    Private WVClient As WebkitWebViewClient
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    WVClient.Initialize(Me,"WebViewClient",WebView1,True)
End Sub

Private Sub WebViewClient_OverrideUrlWithExternalAppIntent2 (WebResourceRequest As WebkitWebResourceRequest, ExternalAppIntent As Intent) As Boolean 'Works from API level 24 and above. WebkitWebResourceRequest library required.
    StartActivity(ExternalAppIntent)
    Return True
End Sub
 
Upvote 0

Setlodi

Member
Thank you, Ivica, and drgottjr. I've come with a workaround. Because I already have a connection to MySQL database for authenticating users, I will save my form submission from JotForm straight to a database. Thereafter I'm reading info from this db within B4A. Now the issue is I have 54 variables containing data from my db and now I must print the same. So, what are my best options here? Save the info in pdf or html? Which one is easy to implement and more stable? I would like to print in this format:

Name: My Name
ID Number: 123123123123
Address: 123 Street, City, Country
Telephone: 123456789
...

I have 54 variables that must be printed in the above format.

Would it be better to create a table and put the data in it?
 
Upvote 0
Top