cdp icon indicating copy to clipboard operation
cdp copied to clipboard

c.Page.PrintToPDF is stuck

Open cbgz121 opened this issue 1 year ago • 2 comments

Hi, when i use this code, screenshot can work well but the pdf cannot. It is stuck this code: "pdfData, err := c.Page.PrintToPDF(ctx, pdfArgs)."

` screenshotName := "screenshot.jpg" screenshotArgs := page.NewCaptureScreenshotArgs(). SetFormat("jpeg"). SetQuality(80) screenshot, err := c.Page.CaptureScreenshot(ctx, screenshotArgs) if err != nil { return err } if err = ioutil.WriteFile(screenshotName, screenshot.Data, 0o644); err != nil { return err }

fmt.Printf("Saved screenshot: %s\n", screenshotName)

pdfName := "page.pdf"
f, err := os.Create(pdfName)
if err != nil {
	return err
}

pdfArgs := page.NewPrintToPDFArgs().
	SetTransferMode("ReturnAsStream") // Request stream.
pdfData, err := c.Page.PrintToPDF(ctx, pdfArgs)
if err != nil {
	return err
}`

cbgz121 avatar Nov 13 '24 06:11 cbgz121

Are you using the example as-is? https://github.com/mafredri/cdp/blob/e5a75ebc0b46b7dbf5553fc2ff6f2b11754d65ea/example_test.go

If yes, can you try to add logging and show the output? Example here: https://github.com/mafredri/cdp/blob/e5a75ebc0b46b7dbf5553fc2ff6f2b11754d65ea/example_logging_test.go#L49-L52

And finally, what browser/version are you using?

mafredri avatar Nov 13 '24 13:11 mafredri

Are you using the example as-is? https://github.com/mafredri/cdp/blob/e5a75ebc0b46b7dbf5553fc2ff6f2b11754d65ea/example_test.go

If yes, can you try to add logging and show the output? Example here:

https://github.com/mafredri/cdp/blob/e5a75ebc0b46b7dbf5553fc2ff6f2b11754d65ea/example_logging_test.go#L49-L52

And finally, what browser/version are you using?

Yes, I am using that example, and I printed the log but it only returned this,

SEND: {"id":6,"method":"Page.printToPDF","params":{"transferMode":"ReturnAsStream"}}

it seems that the response was not received,

But when I manually navigate the browser to another address while the browser is stuck there, it prints out the error log.

RECV: {"method":"Page.frameStartedLoading","params":{"frameId":"5C7CCA45C475EE6A3946B82836BF66FC"}} RECV: {"method":"Page.frameNavigated","params":{"frame":{"id":"5C7CCA45C475EE6A3946B82836BF66FC","loaderId":"3B03AA43CF7D0BF506F8CE955E191711","url":"chrome://version/","domainAndRegistry":"","securityOrigin":"chrome://version","mimeType":"text/html","adFrameStatus":{"adFrameType":"none"},"secureContextType":"Secure","crossOriginIsolatedContextType":"NotIsolated","gatedAPIFeatures":[]},"type":"Navigation"}} RECV: {"method":"DOM.documentUpdated","params":{}} RECV: {"id":6,"error":{"code":-32000,"message":"Printing failed"}}

and i am using version 129.0.6667.0 of chromium

cbgz121 avatar Nov 14 '24 01:11 cbgz121