Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5b20d4406c | ||
|
|
40f683a917 | ||
|
|
ff460fffa1 |
@@ -17,20 +17,17 @@ import (
|
|||||||
|
|
||||||
// html to pdf converter structure for
|
// html to pdf converter structure for
|
||||||
type Converter struct {
|
type Converter struct {
|
||||||
chromePath string
|
chromePath string
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
progress func(progress int)
|
allocCancel context.CancelFunc
|
||||||
|
progress func(progress int)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConverter starts a new converter instance with a chrome headless shell executable
|
// NewConverter starts a new converter instance with a chrome headless shell executable
|
||||||
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,14 +39,13 @@ func NewConverter(chromePath string) (*Converter, error) {
|
|||||||
chromedp.NoSandbox,
|
chromedp.NoSandbox,
|
||||||
chromedp.Headless,
|
chromedp.Headless,
|
||||||
chromedp.DisableGPU,
|
chromedp.DisableGPU,
|
||||||
chromedp.Flag("disable-software-rasterizer", true),
|
|
||||||
chromedp.Flag("disable-dev-shm-usage", true),
|
|
||||||
chromedp.Flag("no-first-run", 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,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 {
|
||||||
@@ -121,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) {
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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")
|
}
|
||||||
)
|
cmd.SysProcAttr.HideWindow = true
|
||||||
|
cmd.SysProcAttr.CreationFlags = 0x08000000 // CREATE_NO_WINDOW
|
||||||
const SW_HIDE = 0
|
})
|
||||||
|
|
||||||
func hideConsole() {
|
|
||||||
hwnd, _, _ := getConsoleWindow.Call()
|
|
||||||
if hwnd != 0 {
|
|
||||||
showWindow.Call(hwnd, SW_HIDE)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user