6 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
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 53 additions and 9 deletions

View File

@@ -17,15 +17,17 @@ import (
// html to pdf converter structure for
type Converter struct {
chromePath string
ctx context.Context
cancel context.CancelFunc
progress func(progress int)
chromePath string
ctx context.Context
cancel context.CancelFunc
allocCancel context.CancelFunc
progress func(progress int)
}
// 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,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
}
@@ -78,11 +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)
defer c.cancel()
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 {
@@ -98,18 +102,28 @@ 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
}
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) {

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