anyone use pdfium from golang?

940 views
Skip to first unread message

Ryan Pendergast

unread,
Apr 26, 2018, 4:31:14 PM4/26/18
to pdfium
Wondering if anyone has experience using pdfium from go.  I know you can compile go programs to use c[++] libs via cgo, however I haven't used c[++] in forever and can't figure out how to write a go program to utilize the pdfium lib.

ninja -C out/build pdfium

produces a obj/libpdfium.a file.  Does anyone have experience with this, that can give me a small example of how I would include libpdfium.a into my go program and call a function?  An example using cgo to compile said program would also be very helpful.

My end goal is to fill out FDF fields in a PDF using go.

thanks in advance

Phil Richards

unread,
Apr 30, 2018, 4:20:58 AM4/30/18
to pdfium
Here's a sample - it provides a pdfium.Context Go type (which has a Close method) and a NewContext ctor. NewContext calls FPDF_InitLibrary and Context.Close calls FPDF_DestroyLibrary. 

package pdfium

/*
#cgo CXXFLAGS: -I${SRCDIR}/include -std=c++11
#cgo CFLAGS: -I${SRCDIR}/include
#cgo LDFLAGS: -L${SRCDIR}/libs -lpdfium
#include <fpdfview.h>
#include <fpdf_edit.h>
#include <fpdf_doc.h>
#include <stdlib.h>
*/
import "C"
import (
"sync"
)

var mut sync.Mutex

type Context struct{}

func NewContext() *Context {
mut.Lock()
defer mut.Unlock()

C.FPDF_InitLibrary()

return &Context{}
}

func (c *Context) Close() error {
mut.Lock()
defer mut.Unlock()

C.FPDF_DestroyLibrary()
return nil
}

//func (c *Context) NewDoc() *Doc {
// mut.Lock()
// defer mut.Unlock()
//
// return &Doc{
// cdoc: C.FPDF_CreateNewDocument(),
// }
//}


The lines above 'import "C"' tell the go tool where to look for headers and library files; 'SRCDIR' refers to the directory that contains the above file. Hopefully that should get you started :)

Jonas Brunsgaard

unread,
Oct 23, 2018, 5:28:14 PM10/23/18
to pdfium

Rodrigo Aliste

unread,
Oct 23, 2018, 5:51:40 PM10/23/18
to jonas.br...@gmail.com, pdf...@googlegroups.com
We use it for rendering page bitmaps into PNG and for metadata extraction. We basically implemented what Jonas already shared, but we took a subprocess approach as we needed random document access and high concurrency (we use it as part of an RPC system).

--
You received this message because you are subscribed to the Google Groups "pdfium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pdfium+un...@googlegroups.com.
To post to this group, send email to pdf...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pdfium/120b471d-2806-466a-a139-a0f6cfc1f18d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Rodrigo Aliste

ral...@webdox.cl

Gerente de Producto (CPO)

+ 56 973330751

Vignan E-Giants

unread,
Jan 22, 2020, 3:20:45 AM1/22/20
to pdfium
The one provided by Jonas is a great starting point to parse pdfs into images.  Wondering if anyone here can provide something similar with FPDF_SaveAsCopy functionality in Go.


On Tuesday, October 23, 2018 at 5:51:40 PM UTC-4, Rodrigo Aliste wrote:
We use it for rendering page bitmaps into PNG and for metadata extraction. We basically implemented what Jonas already shared, but we took a subprocess approach as we needed random document access and high concurrency (we use it as part of an RPC system).

El mar., 23 oct. 2018 a las 18:28, Jonas Brunsgaard (<jonas.b...@gmail.com>) escribió:
I just started working on it.

https://github.com/brunsgaard/go-pdfium-render/



On Thursday, 26 April 2018 22:31:14 UTC+2, Ryan Pendergast wrote:
Wondering if anyone has experience using pdfium from go.  I know you can compile go programs to use c[++] libs via cgo, however I haven't used c[++] in forever and can't figure out how to write a go program to utilize the pdfium lib.

ninja -C out/build pdfium

produces a obj/libpdfium.a file.  Does anyone have experience with this, that can give me a small example of how I would include libpdfium.a into my go program and call a function?  An example using cgo to compile said program would also be very helpful.

My end goal is to fill out FDF fields in a PDF using go.

thanks in advance

--
You received this message because you are subscribed to the Google Groups "pdfium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pdf...@googlegroups.com.

To post to this group, send email to pdf...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pdfium/120b471d-2806-466a-a139-a0f6cfc1f18d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeroen Bobbeldijk

unread,
Feb 5, 2022, 1:45:38 PM2/5/22
to pdfium
Anyone passing by might want to check this out: https://github.com/klippa-app/go-pdfium

It's a Go implementation of PDFium that handles all the cgo stuff for you, and it also allows you to use it in the same process or multi-threaded using a sub-process approach.
I'm working on implementing the full API, but the most important stuff is in there.

FPDF_SaveAsCopy is also implemented here, as is FPDF_CreateNewDocument and FPDF_ImportPages, so you can also use it to merge/split pages, which is probably what the person above me wants to do.

Reply all
Reply to author
Forward
0 new messages