【问题标题】:chromedp - Go - Show invalid printer settings error (-32000) - When setting WithMarginTopchromedp - Go - 显示无效的打印机设置错误 (-32000) - 设置 WithMarginTop 时
【发布时间】:2022-05-18 13:20:23
【问题描述】:

我正在使用 chromedp 并尝试在 puppeteer node.js 中复制该功能,但在 golang 中。

我发现使用 chromedp 时,与 chromium 相同的 JSON 有效负载会导致错误


package main

import (
    "context"
    "io/ioutil"
    "log"

    "github.com/chromedp/cdproto/page"
    "github.com/chromedp/chromedp"
)

func main() {
    // create context
    ctx, cancel := chromedp.NewContext(context.Background())
    defer cancel()

    // capture pdf
    var buf []byte
    if err := chromedp.Run(ctx, printToPDF(`<html><body><h1>Yeeeew!</h1></body></html>`, &buf)); err != nil {
        log.Fatal(err)
    }

    if err := ioutil.WriteFile("sample.pdf", buf, 0644); err != nil {
        log.Fatal(err)
    }
}

// https://github.com/puppeteer/puppeteer/blob/4d9dc8c0e613f22d4cdf237e8bd0b0da3c588edb/src/common/PDFOptions.ts#L74
// https://github.com/puppeteer/puppeteer/blob/4d9dc8c0e613f22d4cdf237e8bd0b0da3c588edb/src/common/Page.ts#L3366
//https://github.com/chromedp/chromedp/issues/836
func printToPDF(html string, res *[]byte) chromedp.Tasks {
    return chromedp.Tasks{
        chromedp.Navigate("about:blank"),
        chromedp.ActionFunc(func(ctx context.Context) error {
            frameTree, err := page.GetFrameTree().Do(ctx)
            if err != nil {
                return err
            }

            return page.SetDocumentContent(frameTree.Frame.ID, html).Do(ctx)
        }),
        chromedp.ActionFunc(func(ctx context.Context) error {
            buf, _, err := page.PrintToPDF().
                // WithPrintBackground(false).
                WithMarginTop(20.0).
                // WithMarginLeft(20).
                // WithMarginBottom(20.0).
                // WithMarginRight(20).
                Do(ctx)
            if err != nil {
                return err
            }
            *res = buf
            return nil
        }),
    }
}

我已经出售了模块并编辑了 cdproto/page/page.go 以打印出发送到 chromium 的 JSON

{"marginTop":20,"marginBottom":0,"marginLeft":0,"marginRight":0,"transferMode":"ReturnAsStream"}

我也在 node.js 中完成了这项工作,并注销了 json 以进行比较


node index.js 
PDF command: 'Page.printToPDF' {
  transferMode: 'ReturnAsStream',
  marginTop: 20,
  marginBottom: 20,
  marginLeft: 0,
  marginRight: 0
}

我不确定为什么会出现此错误?有什么想法吗?

【问题讨论】:

  • 它都是 json 到 chrome 的 api,我什至从 go 中提供了相同的 json 并得到了相同的错误,一定是别的东西

标签: go chromedp


【解决方案1】:

TL;DR

边距值太大。我认为你的意思是通过20.0/96 英寸:

-        WithMarginTop(20.0).
+        WithMarginTop(20.0/96).

说明

从 chromium 返回的错误消息具有误导性。我猜这是发生了什么:提供的边距设置无效,并且由于铬在无头模式下运行,它无法显示错误对话框。错误消息可以解释为“我无法显示对话框来提醒用户提供的打印机设置无效”

原始 CDP 消息中的"marginTop":20 表示顶部有 20 英寸,对于 A4 页面 (A4: 8.27in x 11.7in) 来说太大了。请注意,puppeteer 中的数字被视为像素,并将在发送到 chromium 之前转换为英寸(请参阅https://github.com/puppeteer/puppeteer/blob/4d9dc8c0e613f22d4cdf237e8bd0b0da3c588edb/src/common/Page.ts#L3366-L3402)。所以修复是显而易见的。

顺便说一句,查看原始 CDP 消息的方法很简单:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-12
    • 2010-11-08
    • 2021-03-19
    • 2021-07-26
    • 2015-07-07
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    相关资源
    最近更新 更多