We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
func (c *ChromePdfConverter) Convert(request *pb.HtmlToPdfRequest, localFile string) error { options := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Flag("no-sandbox", false), chromedp.Flag("disable-setuid-sandbox", false), ) // create context allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), options...) defer cancel() // also set up a custom logger ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf)) defer cancel() // capture pdf var buf []byte if err := chromedp.Run( ctx, printToPDF( request.Url, &buf, map[string]interface{}{ "EXPORT-PDF-KEY": request.Authorization, "referer": properties.App.Referer, }), ); err != nil { log.Errorln("chrome run failed", err) return err } chromedp.Sleep(time.Duration(2) * time.Second) if err := os.WriteFile(localFile, buf, 0o644); err != nil { log.Errorln("chrome write file failed", err) return err } return nil }
// print a specific pdf page. func printToPDF(url string, res *[]byte, headers map[string]interface{}) chromedp.Tasks { return chromedp.Tasks{ network.Enable(), network.SetExtraHTTPHeaders(network.Headers(headers)), chromedp.Navigate(url), chromedp.ActionFunc(func(ctx context.Context) error { footer := `
The text was updated successfully, but these errors were encountered:
No branches or pull requests
func (c *ChromePdfConverter) Convert(request *pb.HtmlToPdfRequest, localFile string) error {
options := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("no-sandbox", false),
chromedp.Flag("disable-setuid-sandbox", false),
)
// create context
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), options...)
defer cancel()
// also set up a custom logger
ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
defer cancel()
// capture pdf
var buf []byte
if err := chromedp.Run(
ctx,
printToPDF(
request.Url,
&buf,
map[string]interface{}{
"EXPORT-PDF-KEY": request.Authorization,
"referer": properties.App.Referer,
}),
); err != nil {
log.Errorln("chrome run failed", err)
return err
}
chromedp.Sleep(time.Duration(2) * time.Second)
if err := os.WriteFile(localFile, buf, 0o644); err != nil {
log.Errorln("chrome write file failed", err)
return err
}
return nil
}
// print a specific pdf page.
func printToPDF(url string, res *[]byte, headers map[string]interface{}) chromedp.Tasks {
return chromedp.Tasks{
network.Enable(),
network.SetExtraHTTPHeaders(network.Headers(headers)),
chromedp.Navigate(url),
chromedp.ActionFunc(func(ctx context.Context) error {
footer := `
The text was updated successfully, but these errors were encountered: