Platforms to show: All Mac Windows Linux Cross-Platform
/Mac64bit/WebKit2/HTMLViewer Save As PDF
Required plugins for this example: MBS MacBase Plugin, MBS Mac64bit Plugin, MBS Main Plugin, MBS MacCocoa Plugin, MBS MacControls Plugin
Last modified Mon, 6th Dec 2020.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /Mac64bit/WebKit2/HTMLViewer Save As PDF
Download this example: HTMLViewer Save As PDF.zip
Project "HTMLViewer Save As PDF.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control HTMLViewer1 Inherits HTMLViewer
ControlInstance HTMLViewer1 Inherits HTMLViewer
EventHandler Sub Open()
Me.LoadURL "https://www.monkeybreadsoftware.de/xojo/"
End EventHandler
End Control
Control SaveButton Inherits PushButton
ControlInstance SaveButton Inherits PushButton
EventHandler Sub Action()
Dim w As WKWebViewMBS = HTMLViewer1.WKWebViewMBS
If w = Nil Then
MsgBox "Rnn in Xojo 2002r1 or newer."
Return
End If
// try to get PDF
// Requires Safari 14 to work, e.g. macOS 10.15
Dim error As NSErrorMBS
Dim data As MemoryBlock = w.PDFData(error)
If error <> Nil then
MsgBox error.LocalizedDescription
End If
If data <> Nil Then
// write to file
Dim f As FolderItem = SpecialFolder.Desktop.Child("HTMLViewer Save As PDF.pdf")
Dim b As BinaryStream = BinaryStream.Create(f, True)
b.Write data
b.Close
// open in PDF Viewer
f.Launch
End If
End EventHandler
End Control
Control PrintButton Inherits PushButton
ControlInstance PrintButton Inherits PushButton
EventHandler Sub Action()
Dim w As WKWebViewMBS = HTMLViewer1.WKWebViewMBS
If w = Nil Then
MsgBox "Rnn in Xojo 2002r1 or newer."
Return
End If
// Requires Safari 13 to work, e.g. macOS 10.14
// check preferences
Dim Pref As WKPreferencesMBS = w.Preferences
pref.shouldPrintBackgrounds = False
pref.defaultFontSize = 14
// printInfo has paper format
Dim PrintInfo As NSPrintInfoMBS = NSPrintInfoMBS.sharedPrintInfo
// get print operation object
Dim n As NSPrintOperationMBS = w.printOperation(printinfo)
// with dialogs please
n.showsPrintPanel = True
n.showsProgressPanel = True
// and start it!
Call n.runOperationModalForWindow(Self)
End EventHandler
End Control
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project
See also:
Download this example: HTMLViewer Save As PDF.zip
The items on this page are in the following plugins: MBS Mac64bit Plugin.