Save experiment in postgred integration
This commit is contained in:
parent
4f6369fa27
commit
2342a99405
2 changed files with 46 additions and 0 deletions
45
postgrid/cmd/send-pdf/main.go
Normal file
45
postgrid/cmd/send-pdf/main.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// Command pdf is a chromedp example demonstrating how to capture a pdf of a
|
||||
// page.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/chromedp/cdproto/page"
|
||||
"github.com/chromedp/chromedp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// create context
|
||||
ctx, cancel := chromedp.NewContext(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// capture pdf
|
||||
var buf []byte
|
||||
if err := chromedp.Run(ctx, printToPDF(`https://www.google.com/`, &buf)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile("sample.pdf", buf, 0o644); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("wrote sample.pdf")
|
||||
}
|
||||
|
||||
// print a specific pdf page.
|
||||
func printToPDF(urlstr string, res *[]byte) chromedp.Tasks {
|
||||
return chromedp.Tasks{
|
||||
chromedp.Navigate(urlstr),
|
||||
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||
buf, _, err := page.PrintToPDF().WithPrintBackground(false).Do(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*res = buf
|
||||
return nil
|
||||
}),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue