Can DynaPdf print a HTML file

My desktop app creates a HTML file and saves it, then displays it in a HTMLViewer.
I have purchased DynaPdf (Starter) and want to be able to print either the contents of the HTMLViewer or the HTML file to pdf.
Can this be done and if so can someone point me to an example?

wkhtmltopdf maybe ?

The MBS HTML Viewer has extended printing capabilities too IIRC. Maybe a ping for @Christian Schmitz ?

Printing html to pdf is not possible with DynaPDF. I load my html in my Mac only app into a html viewer, print that into a pdf. In a second step I add a header and page numbers. It’s not very complicated.

DynaPDF is not a rendering engine.
WebKit has one, so using HTMLViewer extensions may get you a PDF.
Or use wkhtmltopdf with shell class.

I have use wkhtmltopdf for many years without much problem. Currently it does not handle grid or flexbox

Do you mean you load your HTML into a Web Browser or Xojo HTMLViewer which is is exactly what I want to do.
Now I have DynaPDF the user can generate all sorts of program output to pdf so understandably I don’t want them to have to go back to third party applications so they can print from their browser.

[quote=481794:@Christian Schmitz]DynaPDF is not a rendering engine.
WebKit has one, so using HTMLViewer extensions may get you a PDF.
Or use wkhtmltopdf with shell class.[/quote]
That sounds like a solution, although package and distribution could be a hassle with at least four different versions to cater for four different OS.
Will look into it, thanks.
Alternatively can I make a screenshot of the html viewer contents and print the image to pdf?
Baring that I will have to create a regular pdf and recreate the HTML elements in regular format from scratch.

Here is an old write up I did about using wkhtmltopdf back in 2012 :slight_smile:

http://forums.realsoftware.com/viewtopic.php?f=23&t=44995&p=250970&hilit=wkhtmltopdf#p250970

Sorry Beatrix, I missed the fact that you were using Mac and pdf is available standard from the print options. Really need something for Windows.

The MBS plugin surely has something to print the html viewer on Windows, too. macOS has PDF support baked in so that I don’t need the DynaPDF plugin for adding page numbers and the header. You only need to make sure that you load the data into the html viewer in the main thread.

Mate, you’re gonna need wkhtmltopdf in the end, trust me. It works on Windows, macOS and Linux, plus Desktop, Web and Console. Did I mention it is free!

This is the Method I wrote to convert HTML to a PDF using wkhtmltopdf:

[code]Protected Function getHTMLToPDFWAD(htmlURL As String, pdfDestination As FolderItem, Orientation As String = “Portrait”) as FolderItem
Dim wkHTMLToPDFApp As FolderItem
Dim tempShell As New Shell
Dim tempString As String
Dim f As FolderItem
Dim NowDate As New Date
Dim ReportTitle As String = app.ApplicationName + " Report " + NowDate.LongDate + " " + NowDate.LongTime

if pdfDestination = nil or not pdfDestination.Parent.Exists then Return nil 'must have a place to send the PDF file

if htmlURL = “” then 'check if a valid URL and not two (space delimited!) ’ or myWebAddress = “” or instr(0, myWebAddress, " ") > 0
Return nil
end if

'we’ve received HTML text rather than a URL, so write it to a file and use that as your URL!
if left(htmlURL, 7) <> “http://” and left(htmlURL, 8) <> “https://” and left(htmlURL, 7) <> “file://” then
f = CommonFolders.getDownloadFolderItemWAD(“ReportFile.html”, True)
if f = nil or not f.Parent.Exists then Return nil
CommonFolders.doStringToFileWAD(htmlURL, f)
if f = nil or not f.Exists then Return nil
htmlURL = f.URLPath
end if

'find the library apps for each platform
#if TargetWin32 then
wkHTMLToPDFApp = app.ExecutableFile.Parent.Child(“Helpers”).Child(“wkhtmltopdf”).Child(“bin”).Child(“wkhtmltopdf.exe”)

#elseif TargetLinux then
wkHTMLToPDFApp = GetFolderItem("/usr/local/bin/wkhtmltopdf", FolderItem.PathTypeShell) 'check if wkhtmltopdf is already installed, if so, use that version
if wkHTMLToPDFApp = nil or not wkHTMLToPDFApp.Exists then 'root in Ubuntu places the app here
wkHTMLToPDFApp = GetFolderItem("/usr/bin/wkhtmltopdf", FolderItem.PathTypeShell) 'check if wkhtmltopdf is already installed, if so, use that version
end if
if wkHTMLToPDFApp = nil or not wkHTMLToPDFApp.Exists then 'try our version
f = app.ExecutableFile.Parent.Child(“Helpers”)
if f <> nil and f.Exists then
f = f.Child(“local”)
if f <> nil and f.Exists then
f = f.Child(“bin”)
if f <> nil and f.Exists then
f = f.Child(“wkhtmltopdf”)
end if
else
f = f.Parent
f = f.Child(“localcentos”)
if f <> nil and f.Exists then
f = f.Child(“bin”)
if f <> nil and f.Exists then
f = f.Child(“wkhtmltopdf”)
end if
end if
end if
If f <> Nil And f.Exists And f.Name = “wkhtmltopdf” Then wkHTMLToPDFApp = f
end if
end if

If wkHTMLToPDFApp <> Nil And wkHTMLToPDFApp.Exists Then 'ensure the app is set to execute
  tempShell.Execute("chmod +x " + wkHTMLToPDFApp.ShellPath)
end if

#elseif TargetMacOS then
#if TargetDesktop then
wkHTMLToPDFApp = app.ExecutableFile.Parent.Parent.Child(“Helpers”).Child(“local”).Child(“bin”).Child(“wkhtmltopdf”)

#ElseIf TargetWeb Or TargetConsole Then
  wkHTMLToPDFApp = app.ExecutableFile.Parent.Child("Helpers").Child("local").Child("bin").Child("wkhtmltopdf")
  
#EndIf

If wkHTMLToPDFApp <> Nil And wkHTMLToPDFApp.Exists Then 'ensure the app is set to execute
  tempShell.Execute("chmod +x " + wkHTMLToPDFApp.ShellPath)
End If

#EndIf

If wkHTMLToPDFApp = Nil Or Not wkHTMLToPDFApp.Exists Then 'check the app exists
Return nil
end if

'run the command in Terminal
tempShell.TimeOut = 200000 'Windows shell times out after 20 seconds, in case app goes rogue
tempString = “–page-size A4 --load-error-handling ignore --enable-smart-shrinking --margin-top 10 --margin-bottom 10 --header-spacing 3 --footer-spacing 3 --header-font-size 10 --header-line --header-center “”” + ReportTitle + “”" --footer-line --footer-font-size 8 --footer-right ““Page [page] of [toPage]”” --encoding UTF8 --orientation " + Orientation + " "
#if TargetWin32 then
tempString = “”"" + wkHTMLToPDFApp.NativePath + “”" " + tempString + " “”" + htmlURL + “”" “”" + pdfDestination.NativePath + “”""

#Else
tempString = wkHTMLToPDFApp.ShellPath + " " + tempString + htmlURL + " " + pdfDestination.ShellPath
#EndIf

tempShell.Execute(tempString)
If (tempShell.ErrorCode <> 0 And tempShell.ErrorCode <> 1) Or pdfDestination = Nil Or Not pdfDestination.Exists Then 'errcode 1=content not found, so OK to continue
MsgBox tempShell.Result
Return nil
else
Return pdfDestination
end if
End Function[/code]