Android Question Printer printhtml with css

hung

Active Member
Licensed User
Longtime User
I created html with css to set print media @page to set A4 landscape, then use printer.printhtml to print.

However, the Android printer preview shows A4 portrait, not landscape. So I have to manually change to landscape and print.

Any solution to change paper size and orientation in printer.printhtml?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Create a WebView and load the html to that WebView.
2. Wait for the PageFinished event.
3.
B4X:
Sub CreatePrinterAttributes As JavaObject
   Dim builder As JavaObject
   builder.InitializeNewInstance("android.print.PrintAttributes.Builder", Null)
   Dim mediaSize As JavaObject
   mediaSize = mediaSize.InitializeStatic("android.print.PrintAttributes.MediaSize").GetField("UNKNOWN_LANDSCAPE")
   Return builder.RunMethod("build", Null)
End Sub

Printer.As(JavaObject).RunMethod("PrintWebView", Array("job name", WebView1, CreatePrinterAttributes"))

Not tested.
 
Upvote 0

hung

Active Member
Licensed User
Longtime User
1. Create a WebView and load the html to that WebView.
2. Wait for the PageFinished event.
3.
B4X:
Sub CreatePrinterAttributes As JavaObject
   Dim builder As JavaObject
   builder.InitializeNewInstance("android.print.PrintAttributes.Builder", Null)
   Dim mediaSize As JavaObject
   mediaSize = mediaSize.InitializeStatic("android.print.PrintAttributes.MediaSize").GetField("UNKNOWN_LANDSCAPE")
   Return builder.RunMethod("build", Null)
End Sub

Printer.As(JavaObject).RunMethod("PrintWebView", Array("job name", WebView1, CreatePrinterAttributes"))

Not tested.
Android returns something like "not working". Perhaps need to "setMediaSize" as unknownn_landscape ? But how?
 
Upvote 0

hung

Active Member
Licensed User
Longtime User
Modified sub as below. Works well now. Thank you @Erel .

B4X:
Sub CreatePrinterAttributes As JavaObject
    Dim builder As JavaObject
    builder.InitializeNewInstance("android.print.PrintAttributes.Builder", Null)
    Dim mediaSize As JavaObject
    mediaSize = mediaSize.InitializeStatic("android.print.PrintAttributes.MediaSize").GetField("UNKNOWN_LANDSCAPE")
   
    builder.RunMethod("setMediaSize", Array(mediaSize))
   
    Return builder.RunMethod("build", Null)
   
End Sub
 
Upvote 0
Top