Created
March 30, 2020 23:05
-
-
Save johnrc/caf1e8600af3508d23fe59edbfe8651f to your computer and use it in GitHub Desktop.
Print pdf from website with Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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