Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
40f683a917 | ||
|
|
ff460fffa1 | ||
|
|
70102e991a | ||
|
|
39fb913f87 | ||
|
|
8df18a243f |
@@ -26,6 +26,7 @@ type Converter struct {
|
|||||||
// 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,6 +40,8 @@ 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.cancel = chromedp.NewContext(allocCtx)
|
||||||
@@ -79,7 +82,6 @@ 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)
|
c.ctx, c.cancel = context.WithTimeout(c.ctx, 60*time.Second)
|
||||||
defer c.cancel()
|
|
||||||
|
|
||||||
var pdfData []byte
|
var pdfData []byte
|
||||||
err = chromedp.Run(c.ctx,
|
err = chromedp.Run(c.ctx,
|
||||||
@@ -98,15 +100,19 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
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