-
Notifications
You must be signed in to change notification settings - Fork 770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Investigate why page.PrintToPDF doesn't work sometimes #74
Comments
I should add I have no idea where this is breaking down as Chrome seems like it supports it and the code looks fine in chromedp. I just don't understand enough about debugging the debug protocol to track down the issue. |
This must have actually been an issue with some other piece of the code. When I stripped it down to the minimum I was able to get it to work. |
Can you explain how you got it to work? |
@pescadosmith if this wasn't working properly before, it was likely due to a mismatch in the version of Chrome being used and the protocol version being generated. Because
$ ./update.sh 61.0.3163.113
BROWSER_PROTO: https://chromium.googlesource.com/chromium/src/+/61.0.3163.113/third_party/WebKit/Source/core/inspector/browser_protocol.json?format=TEXT
JS_PROTO: https://chromium.googlesource.com/v8/v8/+/master/src/inspector/js_protocol.json?format=TEXT
$ ./build.sh
go generate
qtc: 2017/10/06 17:03:05 Compiling *.qtpl template files in directory "templates"
qtc: 2017/10/06 17:03:05 Compiling "templates/domain.qtpl" to "templates/domain.qtpl.go"...
qtc: 2017/10/06 17:03:05 Compiling "templates/extra.qtpl" to "templates/extra.qtpl.go"...
qtc: 2017/10/06 17:03:05 Compiling "templates/file.qtpl" to "templates/file.qtpl.go"...
qtc: 2017/10/06 17:03:05 Compiling "templates/type.qtpl" to "templates/type.qtpl.go"...
qtc: 2017/10/06 17:03:05 Total files compiled: 4
go build
time ./chromedp-gen $@
2017/10/06 17:03:06 skipping command Page.addScriptToEvaluateOnLoad [deprecated]
2017/10/06 17:03:06 skipping command Page.removeScriptToEvaluateOnLoad [deprecated]
2017/10/06 17:03:06 skipping command Page.getCookies [redirect:Network]
2017/10/06 17:03:06 skipping command Page.deleteCookie [redirect:Network]
2017/10/06 17:03:06 skipping command Page.setDeviceMetricsOverride [redirect:Emulation]
2017/10/06 17:03:06 skipping command Page.clearDeviceMetricsOverride [redirect:Emulation]
2017/10/06 17:03:06 skipping command Page.setGeolocationOverride [redirect:Emulation]
2017/10/06 17:03:06 skipping command Page.clearGeolocationOverride [redirect:Emulation]
2017/10/06 17:03:06 skipping command Page.setDeviceOrientationOverride [redirect:DeviceOrientation]
2017/10/06 17:03:06 skipping command Page.clearDeviceOrientationOverride [redirect:DeviceOrientation]
2017/10/06 17:03:06 skipping command Page.setTouchEmulationEnabled [redirect:Emulation]
2017/10/06 17:03:06 skipping command Emulation.setVisibleSize [deprecated]
2017/10/06 17:03:06 skipping command DOM.highlightRect [redirect:Overlay]
2017/10/06 17:03:06 skipping command DOM.highlightNode [redirect:Overlay]
2017/10/06 17:03:06 skipping command DOM.hideHighlight [redirect:Overlay]
2017/10/06 17:03:06 skipping event param Target.detachedFromTarget.targetId [deprecated]
2017/10/06 17:03:06 skipping event param Target.receivedMessageFromTarget.targetId [deprecated]
2017/10/06 17:03:06 skipping command param Target.sendMessageToTarget.targetId [deprecated]
2017/10/06 17:03:06 skipping command param Target.detachFromTarget.targetId [deprecated]
2017/10/06 17:03:06 skipping command param Tracing.start.categories [deprecated]
2017/10/06 17:03:06 skipping command param Tracing.start.options [deprecated]
2017/10/06 17:03:06 skipping domain Console (console) [deprecated]
2017/10/06 17:03:06 running goimports
2017/10/06 17:03:26 running easyjson (stubs)
2017/10/06 17:03:26 running easyjson
2017/10/06 17:03:35 running gofmt
2017/10/06 17:03:35 done.
real 0m28.516s
user 1m27.528s
sys 1m11.184s
go install ../../cdp/... Note that the main action/task code defined in the root |
I don't recall exactly what I was doing but I think I did something with the DOM and once I did that Chrome wouldn't render PDFs via github.com/knq/chromedp or even if I manually triggered it via the browser. I was only doing a POC so I don't have the original code around anymore. |
@kenshaw Thanks 😄 ! It looks like that may have been part of the issue but in addition to that PrintToPDF is not implemented for Chrome when the headless arg isn't passed. I don't know if this could be helped by some documentation as Puppeteer explains this here: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions |
for some reasons none will work for me. the solution posted by kenshaw does not work for me. The solution provided by mc0 did help, however my program hangs just before returning the pdf bytes buffer. the ouput with logs enabled is similar to "Page.frameStoppedLoading","params":{"frameId":"7E6D3A2D9963D5C2D46F6F37B1445161"}}
2018/11/12 22:02:25 <- {"id":12,"method":"Page.printToPDF","params":{}}
2018/11/12 22:02:25 -> {"method":"CSS.mediaQueryResultChanged","params":{}}
2018/11/12 22:02:25 -> {"method":"CSS.mediaQueryResultChanged","params":{}}
2018/11/12 22:02:25 -> {"method":"Page.frameResized","params":{}}
2018/11/12 22:02:25 -> {"method":"CSS.mediaQueryResultChanged","params":{}}
2018/11/12 22:02:25 -> {"method":"CSS.mediaQueryResultChanged","params":{}}
2018/11/12 22:02:25 -> {"method":"Page.frameResized","params":{}}
2018/11/12 22:02:25 -> {"id":12,"result":{"data":"JVBER...AwNjgwNiAwMDAwMCBuIAowMDAwMDA2ODkxIDAwMDAwIG4gCjAwMDAwMDY5NzYgMDAwMDAgbiAKMDAwMDAwNzA2MSAwMDAwMCBuIAowMDAwMDA3MjA1IDAwMDAwIG4gCjAwMDAwMDc4NDcgMDAwMDAgbiAKMDAwMDAwODA0NCAwMDAwMCBuIAowMDAwMDE3MTYzIDAwMDAwIG4gCnRyYWlsZXIKPDwvU2l6ZSAxOAovUm9vdCA1IDAgUgovSW5mbyAxIDAgUj4+CnN0YXJ0eHJlZgoxNzY4OAolJUVPRg=="}}
2018/11/12 22:02:25 -> {"method":"CSS.mediaQueryResultChanged","params":{}}
2018/11/12 22:02:25 -> {"method":"CSS.mediaQueryResultChanged","params":{}}
2018/11/12 22:02:25 -> {"method":"Page.frameResized","params":{}}
2018/11/12 22:02:25 -> {"method":"CSS.mediaQueryResultChanged","params":{}}
2018/11/12 22:02:25 -> {"method":"CSS.mediaQueryResultChanged","params":{}}
2018/11/12 22:02:25 -> {"method":"Page.frameResized","params":{}}
2018/11/12 22:02:25 -> { "method": "Inspector.detached", "params": { "reason": "target_closed"} }
^Csignal: interrupt The source program was package main
import (
"context"
"flag"
"io"
"log"
"os"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
)
func main() {
var url string
var out string
var wait time.Duration
flag.StringVar(&url, "url", "", "set the url to fetch")
flag.StringVar(&out, "out", "-", "set destination output, default to stdout")
flag.DurationVar(&wait, "wait", 4*time.Second, "set a time to wait duration before the the page is captured")
flag.Parse()
var dst io.Writer
{
if out == "-" {
dst = os.Stdout
} else {
f, err := os.Create(out)
if err != nil {
log.Fatal(err)
}
defer f.Close()
dst = f
}
}
var err error
// create context
ctxt, cancel := context.WithCancel(context.Background())
def
```er cancel()
// create chrome instance
c, err := chromedp.New(ctxt,
chromedp.WithLog(log.Printf),
chromedp.WithRunnerOptions(func(opts map[string]interface{}) error {
opts["headless"] = ""
return nil
}),
)
if err != nil {
log.Fatal(err)
}
// run task list
var buf []byte
err = c.Run(ctxt, printToPDF(wait, url, &buf))
if err != nil {
log.Fatal(err)
}
// shutdown chrome
err = c.Shutdown(ctxt)
if err != nil {
log.Fatal(err)
}
// wait for chrome to finish
err = c.Wait()
if err != nil {
log.Fatal(err)
}
log.Println(len(buf))
_, err = dst.Write(buf)
if err != nil {
log.Fatal(err)
}
}
func printToPDF(wait time.Duration, urlstr string, res *[]byte) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(urlstr),
chromedp.Sleep(wait),
pdf(res),
}
}
func pdf(pdfbuf *[]byte) chromedp.Action {
if pdfbuf == nil {
panic("pdfbuf cannot be nil")
}
return chromedp.ActionFunc(func(ctx context.Context, h cdp.Executor) error {
// take page screenshot
buf, err := page.PrintToPDF().Do(ctx, h)
if err != nil {
return err
}
*pdfbuf = buf
return nil
})
} i ended using pupeteer :/ |
is this implemented now? |
Reopening temporarily in case there's a bug here. For what it's worth, there's no implementation required here. All the examples above use https://godoc.org/github.com/chromedp/cdproto/page#PrintToPDF, which should work fine with the latest chromedp version. If it doesn't, there may be a bug to investigate. |
will try to use, possibly next week. |
@mvdan everything works fine |
When I attempt to use page.PrintToPDF().Do() I get the following error:
2017/06/13 16:06:52 -> {"error":{"code":-32000,"message":"PrintToPDF is not implemented"},"id":87}
2017/06/13 16:06:52 PrintToPDF is not implemented (-32000)
I've tried this against Chrome build 59, Canary and the latest build. I was able to print to pdf from the cli with:
~/Downloads/chrome-mac/Chromium.app/Contents/MacOS/Chromium --headless --disable-gpu --print-to-pdf=test.pdf http://google.com/
The text was updated successfully, but these errors were encountered: