Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5b20d4406c | ||
|
|
40f683a917 | ||
|
|
ff460fffa1 | ||
|
|
70102e991a | ||
|
|
39fb913f87 | ||
|
|
8df18a243f |
@@ -17,15 +17,17 @@ import (
|
|||||||
|
|
||||||
// html to pdf converter structure for
|
// html to pdf converter structure for
|
||||||
type Converter struct {
|
type Converter struct {
|
||||||
chromePath string
|
chromePath string
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
progress func(progress int)
|
allocCancel context.CancelFunc
|
||||||
|
progress func(progress int)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConverter starts a new converter instance with a chrome headless shell executable
|
// NewConverter starts a new converter instance with a chrome headless shell executable
|
||||||
func NewConverter(chromePath string) (*Converter, error) {
|
func NewConverter(chromePath string) (*Converter, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
c := &Converter{chromePath: chromePath}
|
c := &Converter{chromePath: chromePath}
|
||||||
chromePath, err = c.getChromePath()
|
chromePath, err = c.getChromePath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -39,9 +41,11 @@ func NewConverter(chromePath string) (*Converter, error) {
|
|||||||
chromedp.DisableGPU,
|
chromedp.DisableGPU,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
opts = append(opts, platformOptions())
|
||||||
|
|
||||||
var allocCtx context.Context
|
var allocCtx context.Context
|
||||||
allocCtx, c.cancel = chromedp.NewExecAllocator(context.Background(), opts...)
|
allocCtx, c.cancel = chromedp.NewExecAllocator(context.Background(), opts...)
|
||||||
c.ctx, c.cancel = chromedp.NewContext(allocCtx)
|
c.ctx, c.allocCancel = chromedp.NewContext(allocCtx)
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,11 +82,11 @@ func (c *Converter) Convert(files ...models.File) error {
|
|||||||
|
|
||||||
htmlURL.WriteString(filepath.ToSlash(absPath))
|
htmlURL.WriteString(filepath.ToSlash(absPath))
|
||||||
|
|
||||||
c.ctx, c.cancel = context.WithTimeout(c.ctx, 60*time.Second)
|
ctx, cancel := context.WithTimeout(c.ctx, 60*time.Second)
|
||||||
defer c.cancel()
|
defer cancel()
|
||||||
|
|
||||||
var pdfData []byte
|
var pdfData []byte
|
||||||
err = chromedp.Run(c.ctx,
|
err = chromedp.Run(ctx,
|
||||||
chromedp.Navigate(htmlURL.String()),
|
chromedp.Navigate(htmlURL.String()),
|
||||||
chromedp.WaitReady("body", chromedp.ByQuery),
|
chromedp.WaitReady("body", chromedp.ByQuery),
|
||||||
chromedp.ActionFunc(func(ctx context.Context) error {
|
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||||
@@ -98,18 +102,28 @@ func (c *Converter) Convert(files ...models.File) error {
|
|||||||
return nil
|
return nil
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.cancel()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save PDF to file
|
// Save PDF to file
|
||||||
if err := os.WriteFile(f.Output, pdfData, 0644); err != nil {
|
if err := os.WriteFile(f.Output, pdfData, 0644); err != nil {
|
||||||
|
c.cancel()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c.cancel()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Converter) Close() {
|
||||||
|
if c.cancel != nil {
|
||||||
|
c.cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// getChromePath checks for system Chrome, else falls back to bundled headless shell
|
// getChromePath checks for system Chrome, else falls back to bundled headless shell
|
||||||
func (c *Converter) getChromePath() (string, error) {
|
func (c *Converter) getChromePath() (string, error) {
|
||||||
|
|
||||||
|
|||||||
10
converter/hideOthers.go
Normal file
10
converter/hideOthers.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
//go:build !windows
|
||||||
|
|
||||||
|
package converter
|
||||||
|
|
||||||
|
import "github.com/chromedp/chromedp"
|
||||||
|
|
||||||
|
func platformOptions() chromedp.ExecAllocatorOption {
|
||||||
|
// Returns a no-op option for Mac/Linux
|
||||||
|
return chromedp.NoSandbox
|
||||||
|
}
|
||||||
20
converter/hideWindows.go
Normal file
20
converter/hideWindows.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
//go:build windows
|
||||||
|
|
||||||
|
package converter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/exec"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/chromedp/chromedp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func platformOptions() chromedp.ExecAllocatorOption {
|
||||||
|
return chromedp.ModifyCmdFunc(func(cmd *exec.Cmd) {
|
||||||
|
if cmd.SysProcAttr == nil {
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{}
|
||||||
|
}
|
||||||
|
cmd.SysProcAttr.HideWindow = true
|
||||||
|
cmd.SysProcAttr.CreationFlags = 0x08000000 // CREATE_NO_WINDOW
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -42,7 +42,7 @@ func TestConvertFiles(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := converter.NewConverter("./assets")
|
c, err := converter.NewConverter("assets/chrome-headless-shell/win64")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user