Print WebViewer on macOS in FileMaker
Today an user send us a script which does printing of a webviewer with some report (in HTML) to a PDF file. The code works pretty well and prints automatically dozens of reports to PDF files:
# Check plugin version installed to use right key
Set Variable [ $_MBS_Version ; Value: MBS("Version") ]
Set Variable [ $_MBS_Version_asNumber ; Value: Substitute ( GetAsNumber ( $_MBS_Version ) ; "?" ; 0 ) ]
If [ MBS("isRegistered") ≠ 1 ]
If [ $_MBS_Version_asNumber >= 7 ]
# Version 7 with newer code
Set Variable [ $_MBS ; Value: MBS("Register"; "ABC"; "Complete"; "5 seats"; 201803; "###") ]
Else
# Version 6 with older code
Set Variable [ $_MBS ; Value: MBS("Register"; "ABC"; "Complete"; "5 seats"; 201702; "xyz") ]
End If
End If
#
# Destination file path, could be a parameter to the script.
Set Variable [ $_Path_Destination_Complete ; Value: "/tmp/test.pdf" ]
# Page to load and print
Set Variable [ $URL ; Value: "https://www.mbsplugins.de" ]
#
# Check plugin version needed for FileMaker 16
If [ GetAsNumber ( Get(ApplicationVersion) ) >= 16 and $_MBS_Version_asNumber < 7,4 ]
Show Custom Dialog [ "Process: Error" ; "Filemaker 16 or greater requires MBS Plugin version 7.4 or higher." ]
Halt Script
End If
#
# Set print parameters
Set Variable [ $_MBS ; Value: MBS( "WebView.SetPrintParameter"; "topMargin"; 0 ) ]
Set Variable [ $_MBS ; Value: MBS( "WebView.SetPrintParameter"; "bottomMargin"; 0 ) ]
Set Variable [ $_MBS ; Value: MBS( "WebView.SetPrintParameter"; "leftMargin"; 0 ) ]
Set Variable [ $_MBS ; Value: MBS( "WebView.SetPrintParameter"; "RightMargin"; 30 ) ]
If [ $_MBS_Version_asNumber >= 7,4 ]
Set Variable [ $x ; Value: 0 ]
Set Variable [ $y ; Value: 100 ]
Set Variable [ $w ; Value: 970 ]
Set Variable [ $h ; Value: 440 ]
# Create a webviewer via plugin to use older WebKit which can print
Set Variable [ $$web ; Value: MBS("Webview.Create"; 0; $x; $y; $w; $h) ]
Set Variable [ $error ; Value: MBS("IsError") ]
If [ $error = 0 ]
# Load URL
Set Variable [ $_MBS ; Value: MBS( "WebView.LoadURL"; $$web; $URL) ]
# Wait for loading
Set Variable [ $_WebViewer_isLoading ; Value: True ]
Loop
Pause/Resume Script [ Duration (seconds): ,2 ]
Exit Loop If [ MBS( "WebView.IsLoading"; $_theWebViewerID ) ≠ 1 ]
End Loop
# print webviewer
Set Variable [ $_MBS ; Value: MBS( "WebView.PrintToFile"; $$web ; $_Path_Destination_Complete ) ]
Set Variable [ $error ; Value: MBS("IsError") ]
# close webviewer
Set Variable [ $r ; Value: MBS( "WebView.Close"; $$web) ]
End If
Else
# Print WebViewer Contents to tmp folder
Set Variable [ $_theWebViewerID ; Value: "web" ]
# print webviewer
Set Variable [ $_MBS ; Value: MBS( "WebView.PrintToFile"; $_theWebViewerID ; $_Path_Destination_Complete ) ]
Set Variable [ $error ; Value: MBS("IsError") ]
End If
#
If [ $error ]
Show Custom Dialog [ "Process: Error" ; List ( "Failed to save file [" & $_Path_Destination_Complete & "]" ; MBS("Text.RemovePrefix"; $_MBS… ]
Halt Script
End If
As you see they use either an existing WebViewer on the layout or use the plugin provided one if possible. As WebKit 2.x does not print, but FileMaker 16 only uses the new version, we have to make a webviewer ourselves via plugin functions using older WebKit 1.x.