diff --git a/converter/converter.go b/converter/converter.go index e773e1e..9ca5065 100644 --- a/converter/converter.go +++ b/converter/converter.go @@ -27,10 +27,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 { @@ -42,13 +38,10 @@ func NewConverter(chromePath string) (*Converter, error) { chromedp.NoSandbox, chromedp.Headless, chromedp.DisableGPU, - chromedp.NoFirstRun, - chromedp.Flag("remote-debugging-port", "0"), - chromedp.Flag("disable-extensions", true), - chromedp.Flag("disable-software-rasterizer", true), - chromedp.Flag("disable-dev-shm-usage", true), ) + opts = append(opts, platformOptions()) + var allocCtx context.Context allocCtx, c.cancel = chromedp.NewExecAllocator(context.Background(), opts...) c.ctx, c.cancel = chromedp.NewContext(allocCtx) diff --git a/converter/hideOthers.go b/converter/hideOthers.go index a0dd4e5..1cfebd2 100644 --- a/converter/hideOthers.go +++ b/converter/hideOthers.go @@ -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 } diff --git a/converter/hideWindows.go b/converter/hideWindows.go index e47d9e4..5d17746 100644 --- a/converter/hideWindows.go +++ b/converter/hideWindows.go @@ -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 + }) } diff --git a/html2pdf_test.go b/html2pdf_test.go index fa117d2..145e68a 100644 --- a/html2pdf_test.go +++ b/html2pdf_test.go @@ -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) }