import web.view; import win.ui; /*DSG{{*/ var winform = win.form(text="WebView2 打印网页,自动修改打印设置";right=966;bottom=622) winform.add() /*}}*/ /* import com.wmi; var printer = com.wmi.get("Select * From Win32_Printer WHERE Name=@name",{ name = "EPSON L360 Series" //查找指定打印机 }) if(printer){ printer.SetDefaultPrinter() //设为默认打印机 } //查找打印机: file://~/example/System/Printer/Win32_Printer.aardio */ //WebView2 用户数据目录。 var userDataDir = web.view.defaultUserDataDir; //获取浏览器配置文件 var pathPreferences = io.joinpath(userDataDir,"EBWebView\Default\Preferences"); //第一次运行需要初始化配置文件 if(!io.exist(pathPreferences)){ //创建 WebView2 生成默认配置文件 var wbTest = web.view(,{userDataDir=userDataDir}) wbTest.close();//关闭 WebView2 控件 while(win.isWindow(wbTest.hwndChrome)){ thread.delay(10) } //这里一定要略等待一下 thread.delay(500); } var wbPreferences = JSON.load(pathPreferences) || {}; var appState = ..table.get("printing",wbPreferences) var printAppState = JSON.tryParse(appState) //不存在会返回 NULL 值 printAppState = table.assignDeep(printAppState , { "version":2, "duplexType":"LONG_EDGE", "mediaSize":{"height_microns":297000,"width_microns":210000}, "marginsType":0, "customMargins":{"marginTop":72,"marginRight":72,"marginBottom":72,"marginLeft":72}, "isColorEnabled":true, "isHeaderFooterEnabled":false, "isLandscapeEnabled":false, "isCollateEnabled":true, "isCssBackgroundEnabled":false, "scaling":"100" }); printAppState = JSON.stringify(printAppState) table.set("printing.print_preview_sticky_settings.appState",printAppState,wbPreferences) JSON.save(pathPreferences,wbPreferences,false);//保存,不需要格式化 //创建浏览器控件,参数 @1 指定窗口或 custom 控件对象。 var wb = web.view(winform,{ //--kiosk-printing 允许直接打钱,--use-system-default-printer 参数使用系统默认打印机 startArguments = ["--kiosk-printing","--use-system-default-printer"]; userDataDir = userDataDir; }) //演示网页,调用 Print.js 可以更方便的控制打印 wb.html = /********
这是一份从页面特定区域打印的报告。
| 姓名 | 邮箱 |
|---|---|
| 张三 | zhangsan@example.com |