5 Commits

Author SHA1 Message Date
Adrian Zürcher
40f683a917 fix hid terminal on windows 2025-12-29 14:59:24 +01:00
Adrian Zürcher
ff460fffa1 add new flags 2025-12-29 12:13:12 +01:00
Adrian Zürcher
70102e991a add new flags 2025-12-29 11:44:07 +01:00
Adrian Zürcher
39fb913f87 fix compile error for other os than windows 2025-12-29 11:20:50 +01:00
Adrian Zürcher
8df18a243f add console hide for windows 2025-12-29 11:07:36 +01:00
4 changed files with 38 additions and 2 deletions

View File

@@ -26,6 +26,7 @@ type Converter struct {
// NewConverter starts a new converter instance with a chrome headless shell executable
func NewConverter(chromePath string) (*Converter, error) {
var err error
c := &Converter{chromePath: chromePath}
chromePath, err = c.getChromePath()
if err != nil {
@@ -39,6 +40,8 @@ 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)
@@ -79,7 +82,6 @@ func (c *Converter) Convert(files ...models.File) error {
htmlURL.WriteString(filepath.ToSlash(absPath))
c.ctx, c.cancel = context.WithTimeout(c.ctx, 60*time.Second)
defer c.cancel()
var pdfData []byte
err = chromedp.Run(c.ctx,
@@ -98,15 +100,19 @@ func (c *Converter) Convert(files ...models.File) error {
return nil
}),
)
if err != nil {
c.cancel()
return err
}
// Save PDF to file
if err := os.WriteFile(f.Output, pdfData, 0644); err != nil {
c.cancel()
return err
}
}
c.cancel()
return nil
}

10
converter/hideOthers.go Normal file
View 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
View 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
})
}

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 {
t.Fatal(err)
}