Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5b20d4406c | ||
|
|
40f683a917 | ||
|
|
ff460fffa1 | ||
|
|
70102e991a |
@@ -20,6 +20,7 @@ type Converter struct {
|
||||
chromePath string
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
allocCancel context.CancelFunc
|
||||
progress func(progress int)
|
||||
}
|
||||
|
||||
@@ -27,10 +28,6 @@ type Converter struct {
|
||||
func NewConverter(chromePath string) (*Converter, error) {
|
||||
var err error
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
hideConsole()
|
||||
}
|
||||
|
||||
c := &Converter{chromePath: chromePath}
|
||||
chromePath, err = c.getChromePath()
|
||||
if err != nil {
|
||||
@@ -44,9 +41,11 @@ func NewConverter(chromePath string) (*Converter, error) {
|
||||
chromedp.DisableGPU,
|
||||
)
|
||||
|
||||
opts = append(opts, platformOptions())
|
||||
|
||||
var allocCtx context.Context
|
||||
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
|
||||
}
|
||||
|
||||
@@ -83,10 +82,11 @@ func (c *Converter) Convert(files ...models.File) error {
|
||||
|
||||
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 cancel()
|
||||
|
||||
var pdfData []byte
|
||||
err = chromedp.Run(c.ctx,
|
||||
err = chromedp.Run(ctx,
|
||||
chromedp.Navigate(htmlURL.String()),
|
||||
chromedp.WaitReady("body", chromedp.ByQuery),
|
||||
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||
@@ -118,6 +118,12 @@ func (c *Converter) Convert(files ...models.File) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Converter) Close() {
|
||||
if c.cancel != nil {
|
||||
c.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// getChromePath checks for system Chrome, else falls back to bundled headless shell
|
||||
func (c *Converter) getChromePath() (string, error) {
|
||||
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
|
||||
package converter
|
||||
|
||||
// hideConsole does nothing on non-Windows systems
|
||||
func hideConsole() {
|
||||
// macOS and Linux don't have the same "console window" concept
|
||||
// that needs manual hiding at runtime like Windows.
|
||||
import "github.com/chromedp/chromedp"
|
||||
|
||||
func platformOptions() chromedp.ExecAllocatorOption {
|
||||
// Returns a no-op option for Mac/Linux
|
||||
return chromedp.NoSandbox
|
||||
}
|
||||
|
||||
@@ -3,21 +3,18 @@
|
||||
package converter
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"syscall"
|
||||
|
||||
"github.com/chromedp/chromedp"
|
||||
)
|
||||
|
||||
var (
|
||||
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
user32 = syscall.NewLazyDLL("user32.dll")
|
||||
getConsoleWindow = kernel32.NewProc("GetConsoleWindow")
|
||||
showWindow = user32.NewProc("ShowWindow")
|
||||
)
|
||||
|
||||
const SW_HIDE = 0
|
||||
|
||||
func hideConsole() {
|
||||
hwnd, _, _ := getConsoleWindow.Call()
|
||||
if hwnd != 0 {
|
||||
showWindow.Call(hwnd, SW_HIDE)
|
||||
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 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user