I want to create pdf from a page and I am using chromedp 
<https://pkg.go.dev/github.com/chromedp/chromedp> to capture inner html 
from url, it takes 3-4 seconds to capture html from url

Now I used this functionality in cron for sending pdf to the customer

We have 25000 merchants and every merchant have many customers. Let suppose 
if 1 merchant has 100 customer and he wants to send email notification with 
attachment(pdf) and then we have total 2500000 customers.

example- In cron we process 1 merchant at a time and sent 100 emails with 
pdf to the customer. Now we capture inner html in loops and it takes 5-6 
seconds to send email notification with pdf to the customer.

So there is any way we can reduce time for capturing inner html from url 
using chromedp

and sometimes it gives error of "context deadline exceeded" and "websocket 
url timeout reached"

I am thinking to use channels for email sending with the attachment but I 
am already using Worker pool with buffered channel 
<https://golangbot.com/buffered-channels-worker-pools/> for running my cron.

There is a sample of a code-

```

func CaptureHtml()(template.HTML, error){

ctx, cancel := chromedp.NewContext(context.Background())

defer cancel()

url := "url_from_where_you want to capture html"

var dataHTML string

err := chromedp.Run(ctx, chromedp.Navigate(url), RunWithTimeOut(&ctx, 30, 
chromedp.Tasks{

chromedp.WaitVisible("#root", chromedp.ByQuery)}))

if err != nil {

log.Fatal(err)

}

fmt.Println("Page loaded successfully")

// Sleep for a brief period to ensure the data rendering is complete 
(adjust timing as needed)

time.Sleep(2 * time.Second)

// Capture the HTML content of the element containing the rendered API data

if err := chromedp.Run(ctx,

chromedp.OuterHTML("#root", &dataHTML, chromedp.NodeVisible),

); err != nil {

log.Fatal(err)

}

CreatePdfInBytes(ctx, dataHTML)

return template.HTML(htmlContent), err

}

func CreatePdfInBytes(ctx context.Context, html string){

var wg sync.WaitGroup

var pdfBuf []byte

navigate := chromedp.Navigate("about:blank")

eventLoader := chromedp.ActionFunc(func(ctx context.Context) error {

loaderctx, cancel := context.WithCancel(ctx)

chromedp.ListenTarget(loaderctx, func(event interface{}) {

if _, ok := event.(*page.EventLoadEventFired); ok {

wg.Done()

cancel()

}

})

return nil

})

setDocContent := chromedp.ActionFunc(func(ctx context.Context) error {

frameTree, err := page.GetFrameTree().Do(ctx)

if err != nil {

return err

}

return page.SetDocumentContent(http://frameTree.Frame.ID 
<http://frametree.frame.id/>, html).Do(ctx)

})

loaderWg := chromedp.ActionFunc(func(ctx context.Context) error {

wg.Wait()

return nil

})

genPdf := chromedp.ActionFunc(func(ctx context.Context) error {

buf, _, err := 
page.PrintToPDF().WithMarginTop(0.3).WithMarginBottom(0.3).WithPrintBackground(true).Do(ctx)

if err != nil {

return err

}

pdfBuf = buf

return nil

})

wg.Add(1)

err := chromedp.Run(ctx, navigate, eventLoader, setDocContent, loaderWg, 
genPdf)

if err != nil {

fmt.Println(err)

}

file, err := os.Create("abc.pdf")

if err != nil {

panic(err)

}

defer file.Close()

// Write the PDF data to the file

_, err = file.Write(pdfBuf)

if err != nil {

panic(err)

}

return

}

```

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/0bd4b4f1-ba47-49b6-9f3c-cb5174baec04n%40googlegroups.com.

Reply via email to