Skip to content

Instantly share code, notes, and snippets.

@smallnest
Forked from MrSaints/go-qthtml2pdf.go
Created February 6, 2018 08:26
Show Gist options
  • Save smallnest/a648729859c33ea178d331b46ace61b8 to your computer and use it in GitHub Desktop.
Save smallnest/a648729859c33ea178d331b46ace61b8 to your computer and use it in GitHub Desktop.
A basic HTML to PDF converter in Golang using Qt WebEngine 5.7. For a more production-ready converter, see: http://www.athenapdf.com/
package main
import (
"flag"
"fmt"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/webengine"
"github.com/therecipe/qt/widgets"
"os"
"time"
)
func main() {
in := flag.String("uri", "https://www.fyianlai.com/", "a URI to convert to PDF")
out := flag.String("out", "output.pdf", "a file path for the generated PDF output")
flag.Parse()
start := time.Now()
app := widgets.NewQApplication(len(os.Args), os.Args)
view := webengine.NewQWebEngineView(nil)
page := view.Page()
view.ConnectLoadStarted(func() {
fmt.Printf("loading : %q\n", *in)
})
view.ConnectLoadFinished(func(ok bool) {
defer fmt.Printf("PDF generated : %q (%s)\n", *out, time.Since(start))
fmt.Printf("load finished : %t\n", ok)q
layout := gui.NewQPageLayout2(gui.NewQPageSize2(0), 0, core.NewQMarginsF2(0.0, 0.0, 0.0, 0.0), 0, core.NewQMarginsF2(0.0, 0.0, 0.0, 0.0))
page.PrintToPdf(*out, layout)
// app.Exit(0)
})
view.Load(core.NewQUrl3(*in, 0))
app.Exec()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment