chromedp / chromedp

A faster, simpler way to drive browsers supporting the Chrome DevTools Protocol.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to start chrome in arm environment?Are there any other plans?

huxin198 opened this issue · comments

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 := `

来自 FOOTER
` buf, _, err := page.PrintToPDF(). WithMarginTop(0.5). WithMarginBottom(0.5). WithDisplayHeaderFooter(true). WithFooterTemplate(strings.ReplaceAll(footer, "FOOTER", properties.App.PdfFooter)). WithPrintBackground(false).Do(ctx) if err != nil { return err } *res = buf return nil }), } } Are there any other plans?