Skip to content

Commit cf8eee7

Browse files
authored
5-SaveDemo.au3
1 parent 9fac232 commit cf8eee7

1 file changed

Lines changed: 135 additions & 0 deletions

File tree

examples/5-SaveDemo.au3

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#AutoIt3Wrapper_UseX64=y
2+
#include <GUIConstantsEx.au3>
3+
#include <WindowsConstants.au3>
4+
5+
#include "..\NetWebView2Lib.au3"
6+
7+
; Global objects
8+
Global $hGUI, $idLabelStatus
9+
10+
Main()
11+
12+
Func Main()
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
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 = _Base64Decode($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 = _NetWebView2_ExportPageData($_g_oWeb, 0, "")
61+
Local $s_HTML_FileFullPath = @ScriptDir & '\5-SaveDemo_result.html'
62+
FileWrite($s_HTML_FileFullPath, $s_HTML)
63+
ShellExecute($s_HTML_FileFullPath)
64+
#EndRegion ; HTML
65+
66+
#Region ; MHTML
67+
Local $s_MHTML = _NetWebView2_ExportPageData($_g_oWeb, 1, "")
68+
Local $s_MHTML_FileFullPath = @ScriptDir & '\5-SaveDemo_result.mhtml'
69+
FileWrite($s_MHTML_FileFullPath, $s_MHTML)
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 ;==>Main
87+
88+
; #FUNCTION# ====================================================================================================================
89+
; Name ..........: _Base64Decode
90+
; Description ...:
91+
; Syntax ........: _Base64Decode($input_string)
92+
; Parameters ....: $input_string - An integer value.
93+
; Return values .: None
94+
; Author ........: trancexx
95+
; Modified ......:
96+
; Remarks .......:
97+
; Related .......:
98+
; Link ..........: http://www.autoitscript.com/forum/topic/81332-base64encode-base64decode
99+
; Example .......: yes
100+
; ===============================================================================================================================
101+
Func _Base64Decode($input_string)
102+
103+
Local $struct = DllStructCreate("int")
104+
105+
Local $a_Call = DllCall("Crypt32.dll", "int", "CryptStringToBinary", _
106+
"str", $input_string, _
107+
"int", 0, _
108+
"int", 1, _
109+
"ptr", 0, _
110+
"ptr", DllStructGetPtr($struct, 1), _
111+
"ptr", 0, _
112+
"ptr", 0)
113+
114+
If @error Or Not $a_Call[0] Then
115+
Return SetError(1, 0, "") ; error calculating the length of the buffer needed
116+
EndIf
117+
118+
Local $a = DllStructCreate("byte[" & DllStructGetData($struct, 1) & "]")
119+
120+
$a_Call = DllCall("Crypt32.dll", "int", "CryptStringToBinary", _
121+
"str", $input_string, _
122+
"int", 0, _
123+
"int", 1, _
124+
"ptr", DllStructGetPtr($a), _
125+
"ptr", DllStructGetPtr($struct, 1), _
126+
"ptr", 0, _
127+
"ptr", 0)
128+
129+
If @error Or Not $a_Call[0] Then
130+
Return SetError(2, 0, "") ; error decoding
131+
EndIf
132+
133+
Return DllStructGetData($a, 1)
134+
135+
EndFunc ;==>_Base64Decode

0 commit comments

Comments
 (0)