-
Notifications
You must be signed in to change notification settings - Fork 21
/
print-pdf.go
46 lines (43 loc) · 1.33 KB
/
print-pdf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"embed"
"fmt"
"github.com/energye/energy/cef"
"github.com/energye/energy/common/assetserve"
"github.com/energye/energy/ipc"
"os"
"path"
)
//go:embed resources
var resources embed.FS
func main() {
//全局初始化 每个应用都必须调用的
cef.GlobalInit(nil, &resources)
//创建应用
cefApp := cef.NewApplication(nil)
//指定一个URL地址,或本地html文件目录
cef.BrowserWindow.Config.Url = "http://localhost:22022/index.html"
cef.BrowserWindow.Config.IconFS = "resources/icon.ico"
cef.BrowserWindow.Config.Title = "Energy 打印PFD预览"
cef.SetBrowserProcessStartAfterCallback(func(b bool) {
fmt.Println("主进程启动 创建一个内置http服务")
//通过内置http服务加载资源
server := assetserve.NewAssetsHttpServer()
server.PORT = 22022
server.AssetsFSName = "resources" //必须设置目录名
server.Assets = &resources
go server.StartHttpServer()
})
ipc.IPC.Browser().SetOnEvent(func(event ipc.IEventOn) {
wd, _ := os.Getwd()
//监听事件
event.On("print-pdf", func(context ipc.IIPCContext) {
bw := cef.BrowserWindow.GetWindowInfo(context.BrowserId())
savePath := path.Join(wd, "example", "browser-print-pdf", "test.pdf")
fmt.Println("当前页面保存为PDF", savePath)
bw.Chromium().PrintToPDF(savePath)
})
})
//运行应用
cef.Run(cefApp)
}