Skip to content

Commit b694369

Browse files
authored
Merge pull request #18 from mlipok/example-SAVE
EXAMPLE: 5-SaveDemo.au3
2 parents 81adf62 + 6a71109 commit b694369

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

examples/5-SaveDemo.au3

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#AutoIt3Wrapper_UseX64=y
2+
#include <GUIConstantsEx.au3>
3+
#include <WindowsConstants.au3>
4+
5+
#include "..\NetWebView2Lib.au3"
6+
7+
; Global objects
8+
Global $hGUI
9+
10+
_Example()
11+
12+
Func _Example()
13+
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc)
14+
#forceref $oMyError
15+
16+
#Region ; GUI CREATION
17+
18+
; Create the GUI
19+
$hGUI = GUICreate("WebView2 .NET Manager - Community Demo", 1000, 800)
20+
21+
; Initialize WebView2 Manager and register events
22+
Local $oWebV2M = _NetWebView2_CreateManager("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0", "", "--disable-gpu, --mute-audio")
23+
$_g_oWeb = $oWebV2M
24+
If @error Then Return SetError(@error, @extended, $oWebV2M)
25+
26+
; create JavaScript Bridge object
27+
Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "")
28+
If @error Then Return SetError(@error, @extended, $oWebV2M)
29+
30+
; initialize browser - put it on the GUI
31+
Local $sProfileDirectory = @TempDir & "\NetWebView2Lib-UserDataFolder"
32+
_NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 0, 0, 0, 0, True, True, True, 1.2, "0x2B2B2B")
33+
34+
; show the GUI after browser was fully initialized
35+
GUISetState(@SW_SHOW)
36+
37+
#EndRegion ; GUI CREATION
38+
39+
; navigate to the page
40+
_NetWebView2_Navigate($_g_oWeb, "https://www.microsoft.com/")
41+
42+
#Region ; PDF
43+
; get Browser content as PDF Base64 encoded binary data
44+
Local $s_PDF_FileFullPath = @ScriptDir & '\5-SaveDemo_result.pdf'
45+
Local $dPDF_asBase64 = _NetWebView2_PrintToPdfStream($_g_oWeb)
46+
47+
; decode Base64 encoded data do Binary
48+
Local $dBinaryDataToWrite = _NetWebView2_DecodeB64($_g_oWeb, $dPDF_asBase64)
49+
50+
; finally save PDF to FILE
51+
Local $hFile = FileOpen($s_PDF_FileFullPath, $FO_OVERWRITE + $FO_UTF8_NOBOM + $FO_BINARY)
52+
FileWrite($hFile, $dBinaryDataToWrite)
53+
FileClose($hFile)
54+
55+
; open PDF file in viewer (viewer which is set as default in Windows)
56+
ShellExecute($s_PDF_FileFullPath)
57+
#EndRegion ; PDF
58+
59+
#Region ; HTML
60+
Local $s_HTML_content = _NetWebView2_ExportPageData($_g_oWeb, 0, "")
61+
Local $s_HTML_FileFullPath = @ScriptDir & '\5-SaveDemo_result.html'
62+
FileWrite($s_HTML_FileFullPath, $s_HTML_content)
63+
ShellExecute($s_HTML_FileFullPath)
64+
#EndRegion ; HTML
65+
66+
#Region ; MHTML
67+
Local $s_MHTML_content = _NetWebView2_ExportPageData($_g_oWeb, 1, "")
68+
Local $s_MHTML_FileFullPath = @ScriptDir & '\5-SaveDemo_result.mhtml'
69+
FileWrite($s_MHTML_FileFullPath, $s_MHTML_content)
70+
ShellExecute($s_MHTML_FileFullPath)
71+
#EndRegion ; MHTML
72+
73+
#Region ; GUI Loop
74+
; Main Loop
75+
While 1
76+
Switch GUIGetMsg()
77+
Case $GUI_EVENT_CLOSE
78+
ExitLoop
79+
EndSwitch
80+
WEnd
81+
82+
GUIDelete($hGUI)
83+
#EndRegion ; GUI Loop
84+
85+
_NetWebView2_CleanUp($oWebV2M, $oJSBridge)
86+
EndFunc ;==>_Example

0 commit comments

Comments
 (0)