Skip to content

Commit

Permalink
Windows: fixed starting chrome, add better default for path
Browse files Browse the repository at this point in the history
This fix skips the chrome --version step as it doesn't work with the Windows Chrome. We simply assume now a version >= 132.

Also, on Windows set the default chrome binary path.

see #75082
  • Loading branch information
martinrode committed Feb 28, 2025
1 parent 811bc3b commit b8ea2a5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
39 changes: 22 additions & 17 deletions html2pdf/chrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"sync"
)
Expand Down Expand Up @@ -51,25 +52,29 @@ func startChrome(ctx context.Context, prog string) (port int, process *os.Proces
return 0, nil, nil, err
}

// Beginning with version 132, old Headless mode has been removed from the
// Chrome binary. Please use the new Headless mode
// (https://developer.chrome.com/docs/chromium/new-headless) or the
// chrome-headless-shell which is a standalone implementation of the old
// Headless mode (https://developer.chrome.com/blog/chrome-headless-shell).
out, err := exec.CommandContext(ctx, prog, "--version").CombinedOutput()
if err != nil {
return 0, nil, nil, err
}
var headlessOld string

var version int
matches := regexp.MustCompile(`.*?(1[0-9]+)\.`).FindAllStringSubmatch(string(out), -1)
if len(matches) > 0 {
version, _ = strconv.Atoi(matches[0][1])
}
// On Windows we cannot run version, so we assume Chrome version >= 132
if runtime.GOOS != "windows" {
// Beginning with version 132, old Headless mode has been removed from the
// Chrome binary. Please use the new Headless mode
// (https://developer.chrome.com/docs/chromium/new-headless) or the
// chrome-headless-shell which is a standalone implementation of the old
// Headless mode (https://developer.chrome.com/blog/chrome-headless-shell).
out, err := exec.CommandContext(ctx, prog, "--version").CombinedOutput()
if err != nil {
return 0, nil, nil, err
}

var headlessOld string
if version < 132 {
headlessOld = "=old"
var version int
matches := regexp.MustCompile(`.*?(1[0-9]+)\.`).FindAllStringSubmatch(string(out), -1)
if len(matches) > 0 {
version, _ = strconv.Atoi(matches[0][1])
}

if version < 132 {
headlessOld = "=old"
}
}

cmd := exec.CommandContext(ctx, prog,
Expand Down
7 changes: 6 additions & 1 deletion html2pdf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"log"
"os"
"runtime"
"strings"
"time"

Expand All @@ -26,7 +27,11 @@ var info infoT
func main() {
prog := golib.GetEnv("")["SERVER_PDF_CHROME"]
if prog == "" {
prog = "chromium"
if runtime.GOOS == "windows" {
prog = `C:\Program Files (x86)\Google\Chrome\Application\chrome.exe`
} else {
prog = "chromium"
}
}

infoS := flag.String("info", "", "JSON with callback info sent by fylr")
Expand Down

0 comments on commit b8ea2a5

Please sign in to comment.