Skip to content

Instantly share code, notes, and snippets.

@MrSaints
Last active January 6, 2020 22:30
Show Gist options
  • Save MrSaints/0dcf12ddebebdbdca9353dc1b12160fb to your computer and use it in GitHub Desktop.
Save MrSaints/0dcf12ddebebdbdca9353dc1b12160fb 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()
}
@ivofreitas
Copy link

Is it working?

If yes, what is the prerequisites to run it?

@vikashvverma
Copy link

It does not work well.

Try for this url: https://www.vikashverma.com/resume.html

@MrSaints
Copy link
Author

MrSaints commented Jan 6, 2020

A much simpler alternative using Puppeteer https://github.com/Courtsite/shuttlepdf (does not require a display server).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment