Skip to content

Instantly share code, notes, and snippets.

@johnrc
Created March 30, 2020 23:05
Show Gist options
  • Save johnrc/caf1e8600af3508d23fe59edbfe8651f to your computer and use it in GitHub Desktop.
Save johnrc/caf1e8600af3508d23fe59edbfe8651f to your computer and use it in GitHub Desktop.
Print pdf from website with Golang
func ExamplePrintToPDF() {
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
var buf []byte
if err := chromedp.Run(ctx,
chromedp.Navigate(`https://godoc.org/github.com/chromedp/chromedp`),
chromedp.ActionFunc(func(ctx context.Context) error {
var err error
buf, _, err = page.PrintToPDF().
WithDisplayHeaderFooter(false).
WithLandscape(true).
Do(ctx)
return err
}),
); err != nil {
panic(err)
}
if err := ioutil.WriteFile("page.pdf", buf, 0644); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment