VMx

vmx.im
啥都学一点的填坑人
  1. 首页
  2. 填坑
  3. 正文

windows server2012r2+playwright-go的使用

2026年2月11日 6点热度 0人点赞 0条评论

golang在1.23版本开始不支持win7内核版本,所以使用 golang 1.22版本
playwright 在1.35也不支持
playwright-go能用的只有0.2000.1版本,

go get github.com/playwright-community/playwright-go@v0.2000.1
go run github.com/playwright-community/playwright-go/cmd/playwright install

当然可以在 go直接集成安装

err := playwright.Install()

打印pdf 代码为:

func printToPDF(browser playwright.Browser, targetUrl string, conf *Config) error {
    // 1. 创建上下文时指定语言为中文 (zh-CN)
    context, err := browser.NewContext(playwright.BrowserNewContextOptions{
        Locale: playwright.String("zh-CN"), // 强制页面使用中文界面
    })
    if err != nil {
        return err
    }
    defer context.Close()

    page, err := context.NewPage()
    if err != nil {
        return err
    }
    defer page.Close()

    // 导航到页面
    if _, err := page.Goto(targetUrl, playwright.PageGotoOptions{
        WaitUntil: playwright.WaitUntilStateNetworkidle, // 等待网络空闲,确保资源加载完
    }); err != nil {
        return err
    }

    // 1. 解析域名 (Host: // 与 / 之间的部分)
    u, _ := url.Parse(targetUrl)
    host := u.Host
    if host == "" {
        // 容错处理:处理不带协议头的字符串
        temp := strings.TrimPrefix(targetUrl, "http://")
        temp = strings.TrimPrefix(temp, "https://")
        host = strings.Split(temp, "/")[0]
    }

    // 2. 过滤掉域名中的冒号(防止端口号导致保存失败)
    host = strings.ReplaceAll(host, ":", "_")

    // 3. 获取当前时间,保留到分钟
    // 格式说明:20060102_1504 代表 年月日_时分
    timeStr := time.Now().Format("20060102_1504")

    // 4. 合成文件名 (例如: github.com_20240520_1430.pdf)
    fileName := fmt.Sprintf("%s_%s.pdf", host, timeStr)
    outputPath := filepath.Join(conf.OutputDir, fileName)
    // 定义页眉模板:显示日期
    headerTpl := `
<div style="font-size: 10px; width: 100%; border-bottom: 1px solid #ccc; padding-bottom: 5px; margin: 0 20px;">
    <span class="date"></span>
</div>`

    // 定义页脚模板:显示 URL
    footerTpl := `
<div style="font-size: 10px; width: 100%; border-top: 1px solid #ccc; padding-top: 5px; margin: 0 20px; display: flex; justify-content: space-between;">
    <span class="url" style="word-break: break-all;"></span>    
</div>`

    // 打印 PDF
    _, err = page.PDF(playwright.PagePdfOptions{
        Path: playwright.String(outputPath),

        Format:              playwright.String("A4"),
        PrintBackground:     playwright.Bool(true), // 打印背景图,否则页面可能变白
        Scale:               playwright.Float(conf.Scale),
        DisplayHeaderFooter: playwright.Bool(true), // 开启页眉页脚
        HeaderTemplate:      playwright.String(headerTpl),
        FooterTemplate:      playwright.String(footerTpl),
        Margin: &playwright.PagePdfOptionsMargin{ // 必须设置边距才能看到页眉页脚
            Top:    playwright.String("1.5cm"),
            Bottom: playwright.String("1.5cm"),
            Left:   playwright.String("1cm"),
            Right:  playwright.String("1cm"),
        },
    })

    return err
}
Post Views: 6
标签: 暂无
最后更新:2026年2月11日

huifei

这个人很懒,什么都没留下

点赞
< 上一篇

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2021 vmx.im. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang