1 Commits

Author SHA1 Message Date
Adrian Zürcher
5b20d4406c fix canelation 2025-12-29 16:48:07 +01:00

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)
} }
@@ -44,7 +45,7 @@ func NewConverter(chromePath string) (*Converter, error) {
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
} }
@@ -81,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 {
@@ -116,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) {