HTML to PDF convert in Go using wkhtmltopdf using C library. Is it possible?

2,314 views
Skip to first unread message

Nikolaj Makhinko

unread,
Mar 19, 2014, 4:21:34 AM3/19/14
to golan...@googlegroups.com
I've tried to connect wkhtmltopdf converter with Cgo, but after several hours it still doesn't work. Probably the cause is my low experience in C. But before I continue digging I would be happy to know if it is possible without pain. Also I would appreciate any advice on how to connect wkhtmltopdf C library with Go.

azuma.4...@gmail.com

unread,
Mar 20, 2014, 5:35:13 AM3/20/14
to golan...@googlegroups.com
+1

yeer

unread,
Mar 20, 2014, 5:35:27 AM3/20/14
to golan...@googlegroups.com
any progress? 


On Wednesday, March 19, 2014 4:21:34 PM UTC+8, Nikolaj Makhinko wrote:

Nikolaj Makhinko

unread,
Mar 20, 2014, 6:25:36 AM3/20/14
to golan...@googlegroups.com
Temporary I've solved it with exec:

cmd := exec.Command("xvfb-run", "wkhtmltopdf", "-", path)
cmd.Stdin = strings.NewReader(raw)

So the issue is still relevant.

Mauro Risonho de Paula Assumpção

unread,
Mar 20, 2014, 10:45:24 AM3/20/14
to azuma.4...@gmail.com, golan...@googlegroups.com

+1

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kevin D

unread,
Mar 20, 2014, 8:23:43 PM3/20/14
to golan...@googlegroups.com
In production, I call it externally the same way. I was going to check out the bindings, but I may be moving over to princexml and it has no bindings.

Jsor

unread,
Mar 20, 2014, 11:35:23 PM3/20/14
to golan...@googlegroups.com
Exactly what errors are you getting? I'm relatively experienced with Cgo so I may be able to help even if I'm not really familiar with the library.

Nikolaj Makhinko

unread,
Mar 23, 2014, 7:34:24 AM3/23/14
to golan...@googlegroups.com
I've included the pdf.h into the go without errors.  I've tried to reproduce the following example https://github.com/clowder/wkhtmltopdf/blob/master/examples/pdf_c_api.c

Just using "C." prefix isn't working. 

bigras...@gmail.com

unread,
Apr 23, 2014, 4:00:03 PM4/23/14
to golan...@googlegroups.com
I reproduced most of the pdf_c_api.c example (I didn't have time to do the callbacks).

And by the way, I think we don't need xvfb-run anymore if we use the static version. See https://code.google.com/p/wkhtmltopdf/issues/detail?id=3

package main

import (
"fmt"
)

//#cgo CFLAGS: -IC:/wkhtmltopdf/include
//#cgo LDFLAGS: -lwkhtmltox -LC:/wkhtmltopdf/lib
// #include <wkhtmltox/pdf.h>
import "C"

func main() {
C.wkhtmltopdf_init(0)

gs := C.wkhtmltopdf_create_global_settings()

globalSettings := [][2]string{
{"out", "out.pdf"},
{"size.paperSize", "letter"},
}
for _, setting := range globalSettings {
C.wkhtmltopdf_set_global_setting(gs, C.CString(setting[0]), C.CString(setting[1]))
}

os := C.wkhtmltopdf_create_object_settings()

objectSettings := [][2]string{
{"footer.center", "[page] / [toPage]"},
}
for _, setting := range objectSettings {
C.wkhtmltopdf_set_object_setting(os, C.CString(setting[0]), C.CString(setting[1]))
}

c := C.wkhtmltopdf_create_converter(gs)

C.wkhtmltopdf_add_object(c, os, nil)

if C.wkhtmltopdf_convert(c) != 1 {
fmt.Println("http error code", C.wkhtmltopdf_http_error_code(c))
}

C.wkhtmltopdf_destroy_converter(c)

C.wkhtmltopdf_deinit()

Nikolaj Makhinko

unread,
Apr 24, 2014, 4:07:42 PM4/24/14
to golan...@googlegroups.com, bigras...@gmail.com
Thank you. Looks promising. I'm trying to reproduce it in Linux environment(Ubuntu and CentOS) but I'm getting an error:
/usr/bin/ld: cannot find -llibwkhtmltox
collect2: ld returned 1 exit status

The lib name was changed according to the Linux installation(x86_64).
/home/go/app/wkhtmltox/lib folder contains following files:
libwkhtmltox.so.0
libwkhtmltox.so.0.12
libwkhtmltox.so.0.12.0

The header looks like:
//#cgo CFLAGS: -I/home/go/app/wkhtmltox/include
//#cgo LDFLAGS: -llibwkhtmltox -L/home/go/app/wkhtmltox/lib
//#include <wkhtmltox/pdf.h>

Bruno Bigras

unread,
Apr 24, 2014, 4:20:08 PM4/24/14
to Nikolaj Makhinko, golan...@googlegroups.com
Could you try :
//#cgo LDFLAGS: -lwkhtmltox -L/home/go/app/wkhtmltox/lib

Nikolaj Makhinko

unread,
Apr 25, 2014, 9:39:24 AM4/25/14
to golan...@googlegroups.com, Nikolaj Makhinko, bigras...@gmail.com
Yes, I tried it. But it doesn't work. 

I've played a little bit with linking and have it working with the following header:

//#cgo CFLAGS: -Iwkhtmltox/include
//#cgo LDFLAGS: -Wl,-rpath, wkhtmltox/lib/libwkhtmltox.so
//#include <wkhtmltox/pdf.h>
import "C"

libwkhtmltox.so was created as symlink to  libwkhtmltox.so.0

Also the following files were copied to /usr/lib folder:
libwkhtmltox.so  
libwkhtmltox.so.0  
libwkhtmltox.so.0.12  
libwkhtmltox.so.0.12.0

Now the question is: how to get rid of the files copying to /usr/lib?

markg...@gmail.com

unread,
May 29, 2015, 2:44:14 PM5/29/15
to golan...@googlegroups.com, mahinko...@gmail.com
Sorry to make a call back to this but were you able to statically link the wkhtmltox library to your golang app?

I run go build --ldflags '-extldflags "-static"' main.go with the following start of main.go:
//#cgo CFLAGS: -I/usr/local/include/wkhtmltox                                                                                                                                                     
//#cgo LDFLAGS: -Wl,-rpath, /usr/local/lib/libwkhtmltox.so                                                                                                                                        
//#include <pdf.h> 

And receive:
# command-line-arguments
/usr/bin/ld: attempted static link of dynamic object `/usr/local/lib/libwkhtmltox.so'
collect2: error: ld returned 1 exit status
/home/mark/.gvm/gos/go1.4/pkg/tool/linux_amd64/6l: running gcc failed: unsuccessful exit status 0x100

Apologies if the answer is obvious. I'm just unfamiliar with the gcc and linking go and C.

Ian Lance Taylor

unread,
May 29, 2015, 2:55:48 PM5/29/15
to markg...@gmail.com, golang-nuts, mahinko...@gmail.com
On Fri, May 29, 2015 at 10:30 AM, <markg...@gmail.com> wrote:
>
> Sorry to make a call back to this but were you able to statically link the
> wkhtmltox library to your golang app?
>
> I run go build --ldflags '-extldflags "-static"' main.go with the following
> start of main.go:
> //#cgo CFLAGS: -I/usr/local/include/wkhtmltox
> //#cgo LDFLAGS: -Wl,-rpath, /usr/local/lib/libwkhtmltox.so
> //#include <pdf.h>
>
> And receive:
> # command-line-arguments
> /usr/bin/ld: attempted static link of dynamic object
> `/usr/local/lib/libwkhtmltox.so'
> collect2: error: ld returned 1 exit status
> /home/mark/.gvm/gos/go1.4/pkg/tool/linux_amd64/6l: running gcc failed:
> unsuccessful exit status 0x100
>
> Apologies if the answer is obvious. I'm just unfamiliar with the gcc and
> linking go and C.

You can not statically link a .so file. You need a .a file. If you
are installing from a package manager such as apt-get or yum, the .a
file is typically packaged separately in a -devel package.

Ian
Reply all
Reply to author
Forward
0 new messages