Skip to content

Commit

Permalink
html2pdf: fixed on Windows
Browse files Browse the repository at this point in the history
On Windows Chrome was not killed, so the program producing the PDF never finished.

see #72670
  • Loading branch information
martinrode committed Jun 7, 2024
1 parent 2025766 commit 3e69fe6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
7 changes: 2 additions & 5 deletions html2pdf/chrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,8 @@ func startChrome(ctx context.Context, prog string) (port int, process *os.Proces
log.Printf("started %s, waiting to finish...", prog)
err = cmd.Wait()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok && exiterr.ExitCode() == 143 {
// this is a clean exit by SIGTERM
} else {
log.Fatalf("%s existing with error: %s", prog, err.Error())
}
log.Printf("%s exited with error: %s", prog, err.Error())
return
}
log.Printf("%s exited", prog)
}()
Expand Down
8 changes: 0 additions & 8 deletions html2pdf/info.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package main

type config struct {
Config map[string]map[string]any `json:"config,omitempty"`
}

// infoT as received via the command line
type infoT struct {
ApiURL string `json:"api_url"`
ExternalURL string `json:"external_url"`
ApiToken string `json:"api_user_access_token"`
Config struct {
System config `json:"system,omitempty"`
Plugin map[string]config `json:"plugin,omitempty"`
}
}
17 changes: 14 additions & 3 deletions html2pdf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"log"
"os"
"strings"
"syscall"
"time"

"github.com/programmfabrik/golib"
Expand Down Expand Up @@ -80,11 +79,16 @@ func main() {
}

_, err = f.Write([]byte(body.Document))
if err != nil {
f.Close()
endWithError(err)
}
err = f.Close()
if err != nil {
endWithError(err)
}

log.Printf("opened file %s", f.Name())
log.Printf("wrote %d bytes into temp file %s", len(body.Document), f.Name())

data, err := createPdf(ctx, "file://"+f.Name(), port, body.Properties)
if err != nil {
Expand All @@ -105,7 +109,14 @@ func main() {
"pdf size": golib.HumanByteSize(uint64(n)),
}})
log.Printf("%d bytes written to stdout", n)
process.Signal(syscall.SIGTERM)

log.Printf("killing pid %d (chrome)...", process.Pid)
err = process.Kill()
if err != nil {
log.Printf("kill error: %s", err.Error())
} else {
log.Printf("killed")
}

// process.Wait produces an error here with chrome

Expand Down

0 comments on commit 3e69fe6

Please sign in to comment.