2 Commits

Author SHA1 Message Date
Adrian Zürcher
5b20d4406c fix canelation 2025-12-29 16:48:07 +01:00
Adrian Zürcher
40f683a917 fix hid terminal on windows 2025-12-29 14:59:24 +01:00
4 changed files with 34 additions and 35 deletions

View File

@@ -20,6 +20,7 @@ type Converter struct {
chromePath string chromePath string
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
allocCancel context.CancelFunc
progress func(progress int) progress func(progress int)
} }
@@ -27,10 +28,6 @@ type Converter struct {
func NewConverter(chromePath string) (*Converter, error) { func NewConverter(chromePath string) (*Converter, error) {
var err error var err error
if runtime.GOOS == "windows" {
hideConsole()
}
c := &Converter{chromePath: chromePath} c := &Converter{chromePath: chromePath}
chromePath, err = c.getChromePath() chromePath, err = c.getChromePath()
if err != nil { if err != nil {
@@ -42,16 +39,13 @@ func NewConverter(chromePath string) (*Converter, error) {
chromedp.NoSandbox, chromedp.NoSandbox,
chromedp.Headless, chromedp.Headless,
chromedp.DisableGPU, 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 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
} }
@@ -88,10 +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 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 {
@@ -123,6 +118,12 @@ func (c *Converter) Convert(files ...models.File) error {
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) {

View File

@@ -2,8 +2,9 @@
package converter package converter
// hideConsole does nothing on non-Windows systems import "github.com/chromedp/chromedp"
func hideConsole() {
// macOS and Linux don't have the same "console window" concept func platformOptions() chromedp.ExecAllocatorOption {
// that needs manual hiding at runtime like Windows. // Returns a no-op option for Mac/Linux
return chromedp.NoSandbox
} }

View File

@@ -3,21 +3,18 @@
package converter package converter
import ( import (
"os/exec"
"syscall" "syscall"
"github.com/chromedp/chromedp"
) )
var ( func platformOptions() chromedp.ExecAllocatorOption {
kernel32 = syscall.NewLazyDLL("kernel32.dll") return chromedp.ModifyCmdFunc(func(cmd *exec.Cmd) {
user32 = syscall.NewLazyDLL("user32.dll") if cmd.SysProcAttr == nil {
getConsoleWindow = kernel32.NewProc("GetConsoleWindow") cmd.SysProcAttr = &syscall.SysProcAttr{}
showWindow = user32.NewProc("ShowWindow")
)
const SW_HIDE = 0
func hideConsole() {
hwnd, _, _ := getConsoleWindow.Call()
if hwnd != 0 {
showWindow.Call(hwnd, SW_HIDE)
} }
cmd.SysProcAttr.HideWindow = true
cmd.SysProcAttr.CreationFlags = 0x08000000 // CREATE_NO_WINDOW
})
} }

View File

@@ -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)
} }