PrintToPdf
Interface AntView
Type: Method
Parameters: String resultFilePath String printSettings
Returns: nothing
Will print the currently displayed document in the WebView2 control to a file specified by variable resultFilePath.
If resultFilePath is empty then invoking this command will popup a save as dialog to determine where the user can save the file.
The printing process is asynchronously and will return immediately without waiting for the result.
If an invalid file location is passed then the print will fail silently.
You can specify a number of settings for the pdf in the printSettings variable.
The settings are set via JSON and you can pass an empty string to use the defaults.
All parameters are optional and they are as follows:
displayHeaderFooter : boolean
Display Header and Footer.
Defaults to false
footerURI : string
The URI in the footer, when displayHeaderFooter is enabled.
If set to an empty string then the footer is also empty.
Defaults to URI of the current document.
headerTitle : string
The Title in the header, when displayHeaderFooter is enabled.
If set to an empty string then the header is also empty.
Defaults to the title of the current document.
landscape : boolean
Page orientation which is portrait or landscape.
Defaults to false
marginBottom : number
The bottom margin in inches, cannot be less than 0.
Defaults to 1 cm, or ~ 0.4 inches.
marginLeft : number
The left margin in inches, cannot be less than 0.
Defaults to 1 cm, or ~ 0.4 inches
marginRight : number
The right margin in inches, cannot be less than 0.
Defaults to 1 cm, or ~ 0.4 inches
marginTop : number
The top margin in inches, cannot be less than 0.
Defaults to 1 cm, or ~ 0.4 inches
pageHeight : number
The page height in inches, must be greater than 0.0.
Defaults to 11.0
pageWidth : number
The page width in inches, must be greater than 0.0.
Defaults to 8.5
printBackground : boolean
Print background colors and images.
Defaults to false
printSelectionOnly : boolean
Print only what the user has selected in the document.
Defaults to false
scale : number
A scale factor used for rendering, must be between 0.1 and 2.0.
This mostly affects the text size.
Defaults to 1.0
Here's an example of how calling TextToPdf looks like in DataFlex:
Procedure OnClick
String sFilePath
String sSettings
Move "" To sFilePath // not specifying a path+filename will popup a file save as dialog
Move ' { ' to sSettings
Append sSettings '"displayHeaderFooter" : true, '
Append sSettings '"headerTitle" : "AntView PrintToPdf Demo", '
Append sSettings '"printBackground" : true, '
Append sSettings '"scale" : 0.5 '
Append sSettings ' }'
Send ComPrintToPdf of oEdgeWebBrowser sFilePath sSettings
End_Procedure
Beware of correctly formatting the JSON, or else changing the pdf settings do not work.
That means double-quote characters around the name in the JSON name/value pair and a comma between those, except for the last name/value pair.
Eg. the JSON above looks like this as a string (line breaks added for readability):
' { "displayHeaderFooter" : true,
"headerTitle" : "AntView PrintToPdf Demo",
"printBackground" : true,
"scale" : 0.5
}'
There's a OnPrintToPdfCompleted event that triggers when the printing completes.
Known issue: If you call PrintToPdf directly from the OnNavigationCompleted event, then you must add a 2 second wait before calling the PrintToPdf method. See also the following WebView2 github issue 1974. This issue was closed and reported as fixed at June 8, 2022.
AntView - The MS Edge WebView2 ActiveX control Date last changed: 09/30/2024