Skip to content

wassup05/poppler-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

poppler-go

Go Reference

An easy to use and intuitive PDF library using Go and Poppler

Features

  • Implementation of many poppler-glib public C API methods such as

    • Loading Documents (encrypted as well) (from path, os.File, bytes)
    • Getting Document Info such as metadata, page count, attachment count, PDF version, permissions, keywords, subject, title, author, creation date, modification date etc
    • Setting Document Info such as metadata, title, author, subject, keywords, title, author, craetion date, modification date and many more
    • Getting Pages from a Document by their index or label.
      • Getting PageInfo such as page text, size, thumbnail, images, attachments, annotations and so on
      • Searching text, getting text from a certain region, selecting text etc
      • Rendering a Page to Cairo surfaces for printing and viewing.
      • Getting Media on a page such as videos and saving them.
    • Dealing with forms.
    • Getting all the info about fonts, layers, index in a document.
    • Saving the modified Document at a path
    • Rendering to a PostScript File
  • Integration with go-cairo

Poppler

Poppler is a library for rendering PDF files and examining, modifying their structure. This repository contains Go Bindings for the stable C api of poppler (glib) to examine and modify the structure of PDF files and render it's pages to Cairo contexts.

Why?

I once thought about making a tui app for viewing PDF's in go and hence went on a search for PDF rendering, manipulation libraries and saw that poppler was one of the many choices available... But it's go bindings cheggaaa/go-poppler and multitude of it's forks although quite suitable for simple tasks was not as complete as it's other language counterparts, So I thought about making a more complete version of it.

Although there are some edges here and there, I plan on making them as smooth as possible.

Requirements

  • Go
  • poppler library
  • cairo library

For Debian/Ubuntu

sudo apt install libpoppler-dev libcairo2-dev

For Fedora

sudo dnf install poppler-glib-devel cairo-devel

For Arch

sudo pacman -S poppler poppler-data poppler-glib cairo

Official source: poppler, cairo

Examples of common tasks

Run the following command to add the library to your go.mod and look at the examples below

go get github.com/wassup05/poppler-go
  • Dealing with PDF metadata
package demo

import (
    "fmt"
    "github.com/wassup05/poppler-go"
)

func main(){
    path := "/path/to/pdf"
    
    doc, err := poppler.NewDocFromFilename(path)
    defer doc.Close()

    if err != nil {
        fmt.Printf("Error opening PDF: %s\n",err.Message)
    }

    fmt.Printf("page count:%d\n",doc.GetPageCount())
    fmt.Printf("author:%s\n",doc.GetAuthor())
    fmt.Printf("creator:%s\n",doc.GetCreator())
    fmt.Printf("keywords:%s\n",doc.GetKeywords())

    doc.SetTitle("blahblah")

    fmt.Printf("title:%s\n",doc.GetTitle()) // blahblah

    info := doc.GetInfo()

    fmt.Printf("info:%#v\n",info) // a convinience method to get all the useful info in one go as a struct
}
  • Getting the pages of a document and it's info
package demo

import (
    "fmt"
    "github.com/wassup05/poppler-go"
)

func main(){
    path := "/path/to/pdf"
    
    doc, err := poppler.NewDocFromFilename(path)
    defer doc.Close()

    if err != nil {
        fmt.Printf("Error opening PDF: %s\n",err.Message)
    }

    page := doc.GetPage(2)
    defer page.Close()

    label := page.GetLabel() // gets the label of the page
    height, width := page.GetSize() // gets the height, width of the page
    text := page.GetText() // gets all the text on the page

    fmt.Printf("label:%s\nheight,width: %f, %f\ntext: %s\n",label,height,width,text)

    pageInfo := page.GetInfo() // convinience method

    fmt.Printf("Page Info:%#v\n",pageInfo)
}
  • Rendering a PDF page to a SVG
package demo

import (
    "github.com/ungerik/go-cairo"
    "github.com/wassup05/poppler-go"
)

func main(){
	doc, err := poppler.NewDocFromFilename("abs/path/to/pdf")
    defer doc.Close()

	if err != nil {
        fmt.Printf("Error opening PDF: %s\n",err.Message)
	}

	page := doc.GetPage(0) // get the first page
    defer page.Close()

	h, w := page.GetSize()

	surface := cairo.NewSVGSurface("test.svg", w, h, cairo.SVG_VERSION_1_2)

	page.RenderToSurface(surface)

	surface.Destroy() // a svg with name `test.svg` with the contents of the 1st page will be created
}
  • Dealing with attachments
package demo

import (
    "fmt"
    "github.com/wassup05/poppler-go"
)

func main(){
	doc, err := poppler.NewDocFromFilename("abs/path/to/pdf")
    defer doc.Close()

	if err != nil {
        fmt.Printf("Error opening PDF: %s\n",err.Message)
	}

    attachments := doc.GetAttachments()

    for i, a := range attachments {
        fmt.Printf("name:%s\n",a.GetName())
        fmt.Printf("description:%s\n",a.GetDescription())

        saveErr := a.Save(fmt.Sprintf("demo-%d",i)) // saves the attachment to the path

        if saveErr != nil {
            // handle the error
        }
    }
}

Missing Features that ARE present in poppler

  • Advanced Interaction with form fields, annotations and page structures, Which will be implemented soon
  • Extensive Documentation

Credits

Contributing

check this out

License

GNU General Public License, Version 3.0 (GPL v3.0)

About

Go Bindings for the poppler PDF library (glib)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages