gokul656
gokul656

Reputation: 313

Render HTML and save as PDF with Golang

I'm trying to convert a local HTML file to a PDF. For that currently,

google-chrome --headless --print-to-pdf="report.pdf" template.html

I'm executing the above-mentioned command via the terminal to generate PDF. It's working fine but without chrome installed on the running device, it won't work. I don't want to use WKHTMLTOPDF or any other installed browsers either. But I can use a webkit. Are there any other ways to do this using Golang?

Thanks in advance!

Upvotes: 0

Views: 5104

Answers (2)

user3919706
user3919706

Reputation: 115

You have to look for anything like the --print-to-pdf provided by chrome on your webkit. If I recall correctly QT version used to have something like that and WKHTMLTOPDF is just a thin C++ CLI wrapper for it. But then if you would just do the same on Go, so why not use the tool that is already there?

If you want to convert a html template to PDF you pretty much have use a browser, there is no way around it, unless you want to build an entire CSS + HTML render engine on Go just to generate your PDFs. There is a python project that does exactly that.

Chromedp gives you a lot of options to render your pdf as you can see here and you can build a headless version of chromium or use an already built docker image.

If you absolutely won't run a browser your probably best option is to generate your PDFs template straight on Go using a PDF processor such as instead of generating them from HTML.

Upvotes: 2

vovs03
vovs03

Reputation: 41

You can use Golang-HTML-TO-PDF-Converter:

Upvotes: 0

Related Questions