Method XbpHTMLViewer2():print() Foundation

Prints the current page.

Syntax
:print( <oPrintSettings> ) --> oAsyncResult
Parameters
<oPrintSettings>
A CoreWebView2PrintSettings() object with the desired settings for the print job. This parameter is optional. If no object is passed, the default settings are used and the output is sent to the system's default printer.
Return

A AsyncResult() object which can be used for waiting for printing to complete and for retrieving the operation result. This is a numeric status code. See the method description and the example below for further information.

Description

The contents on the current page can be sent to any of the installed printers using the :print() method. In addition, page contents can be written into a PDF file using :printToFile().

Various aspects of the print operation can be configured via an optional CoreWebView2PrintSettings object which exposes various WebView2-specific output settings.

Only one print or output job can be active at a given time. The output is performed asynchronously. :print() returns a AsyncResult() object which can be used to wait for printing to complete and for retrieving the status of the operation. The operation's status code is returned by AsyncResult():get() and can be used as demonstrated in the example below. The documentation of the WebView2-specific status code is available here: https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/webview2-idl?view=webview2-1.0.3485.44#corewebview2_print_status.

Example for printing a webpage

// This example demonstrates printing a webpage on a application-defined 
// printer with a certain number of copies. The example uses a 
// CoreWebView2Settings object to set the desired properties for the 
// print job, and outputs information on whether printing succeeded. 
#include "xbp.ch" 

#define E_INVALIDARG                                  0x80070057 
#define E_ABORT                                       0x80004004 
#define COREWEBVIEW2_PRINT_STATUS_SUCCEEDED           0 
#define COREWEBVIEW2_PRINT_STATUS_PRINTER_UNAVAILABLE 1 

PROCEDURE Main() 
LOCAL oDlg 
LOCAL oViewer2 
LOCAL oPrintSettings 
LOCAL oAsyncResult 
LOCAL cMsg 

  SET CHARSET TO ANSI 

  // Create a form for hosting the HTML viewer object 
  oDlg := XbpDialog():new( AppDesktop() ) 
  oDlg:taskList := .T. 
  oDlg:title    := "XbpHTMLViewer2:print() example" 
  oDlg:create( ,,, {640,480} ) 
  CenterControl( oDlg ) 

  // Create the HTML viewer object in the form's content area 
  oViewer2 := XbpHTMLViewer2():new( oDlg:drawingArea ) 
  oViewer2:layoutAlign := XBPLAYOUT_LEFT + XBPLAYOUT_TOP + XBPLAYOUT_RIGHT + XBPLAYOUT_BOTTOM 
  oViewer2:create( ,, {10,10}, {600,420} ) 

  // Navigate to a webpage. Navigation occurs asynchronously 
  // and wait() is used to wait for the operation to complete 
  oViewer2:navigate( "https://doc.alaska-software.com" ):wait() 

  // Specify desired print job properties using a print 
  // settings object: 
  //  - destination printer 
  //  - number of copies 
  oPrintSettings := CoreWebView2PrintSettings():new() 
  oPrintSettings:printerName := "Microsoft Print to PDF" 
  oPrintSettings:copies      := 2 
  oAsyncResult := oViewer2:print( oPrintSettings ) 

  // Determine the status of the print job 
  cMsg := GetPrintStatus( oAsyncResult ) 
  MsgBox( cMsg, "Print" ) 

  oDlg:showModal() 
  oDlg:destroy() 
RETURN 

// Determine the status of the last print using the 
// async result object returned by the operation. 
// Return is a string with status information. 
FUNCTION GetPrintStatus( oAsyncResult ) 
LOCAL nValue 
LOCAL cRet 

  nValue := oAsyncResult:get() 

  // A result state other than "finished" indicates 
  // the operation itself has failure. In both this and 
  // in the success case, the operation result (nValue) 
  // contains further information. See the documentation 
  // on :print() for details. 
  IF oAsyncResult:state() == "finished" 
     IF nValue == COREWEBVIEW2_PRINT_STATUS_SUCCEEDED 
        cRet := "The page was printed successfully" 
     ELSEIF nValue == COREWEBVIEW2_PRINT_STATUS_PRINTER_UNAVAILABLE 
        cRet := "The printer doesn't exist, is in an error state or is offline" 
     ELSE 
        cRet := "Printing finished with an unknown status" 
     ENDIF 
  ELSE 
     // Again, check nValue for details! 
     cRet := "Serious print error" 
  ENDIF 
RETURN cRet 

PROCEDURE AppSys() 
  // Prevent creation of the default console window 
RETURN 

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.