Skip to content
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

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

Open
huxin198 opened this issue Apr 10, 2024 · 0 comments
Open

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

huxin198 opened this issue Apr 10, 2024 · 0 comments

Comments

@huxin198
Copy link

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?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant