lazypdf

package module
v0.0.0-...-72f35e0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2021 License: AGPL-3.0 Imports: 9 Imported by: 1

README

LazyPDF

This began as a rollback fork of https://github.com/Nitro/lazypdf to return it to use as a library.

This is a rasterizing engine for PDF documents, built around MuPDF. It exports a Go interface that allows the creation of a Rasterizer for each document you wish to be able to rasterize to images. It generated Go image.Images and you can then render these as PNG/JPEG/etc by using the Go stdlib functions for doing that (image/jpeg, image/png).

Building

You can simply run ./build and the shell script will do all the right things on either Linux or OSX.

Installing

You cannot simply go get this library. It pulls in and links against C code which is external to this repository for licensing reasons (the MuPDF library is AGPL). Thus, in order to build this library, you need to run the build shell script to pull down and compile MuPDF first.

Anything that you then want to link against this library will need to use the correct Cgo configuration. That should look like:

// #cgo CFLAGS: -I. -I./mupdf-1.12.0-source/include -I./mupdf-1.12.0-source/include/mupdf -I./mupdf-1.12.0-source/thirdparty/openjpeg -I./mupdf-1.12.0-source/thirdparty/jbig2dec -I./mupdf-1.12.0-source/thirdparty/zlib -I./mupdf-1.12.0-source/thirdparty/jpeg -I./mupdf-1.12.0-source/thirdparty/freetype -g
// #cgo LDFLAGS: -L./mupdf-1.12.0-source/build/release -lmupdf -lmupdfthird -lm -ljbig2dec -lz -lfreetype -ljpeg -lcrypto -lpthread
// #include <faster_raster.h>
import "C"

<relative path> in this case should be the relative location of this library to your project. If you are building a Go project under the github.com/Nitro folder, this would usually be ../lazypdf.

Documentation

Overview

Package lazypdf provides a MuPDF-based document page rasterizer. It is managed via the Rasterizer struct.

Index

Constants

View Source
const (
	// We'll wait up to 10 seconds for a single page to Rasterize.
	RasterTimeout = 10 * time.Second

	LandscapeScale = 1.0
	PortraitScale  = 1.5
)
View Source
const (
	RasterImage rasterType = iota
	RasterSVG
)

Variables

View Source
var (
	// A page was requested with an out of bounds page number.
	ErrBadPage = errors.New("invalid page number")
	// We tried to rasterize the page, but we gave up waiting.
	ErrRasterTimeout = errors.New("rasterizer timed out!")
)

Functions

func IsBadPage

func IsBadPage(err error) bool

IsBadPage validates that the type of error was an ErrBadPage.

func IsRasterTimeout

func IsRasterTimeout(err error) bool

IsRasterTimeout validates that the type of error was an ErrRasterTimeout.

Types

type RasterImageReply

type RasterImageReply struct {
	RasterReply
	Image image.Image
}

type RasterReply

type RasterReply struct {
	// contains filtered or unexported fields
}

func (*RasterReply) Error

func (r *RasterReply) Error() error

type RasterRequest

type RasterRequest struct {
	PageNumber int
	Width      int
	Scale      float64
	RasterType rasterType
	ReplyChan  chan ReplyWrapper
	// contains filtered or unexported fields
}

type RasterSVGReply

type RasterSVGReply struct {
	RasterReply
	SVG []byte
}

type Rasterizer

type Rasterizer struct {
	Filename    string
	RequestChan chan *RasterRequest
	Ctx         *C.struct_fz_context_s
	Document    *C.struct_fz_document_s
	// contains filtered or unexported fields
}

Rasterizer is an actor that runs on an event loop processing a request channel. Replies are asynchronous from the standpoint of the internals of the library but the exported interface (via GeneratePage) is synchronous, using channels.

If you need to use this asynchronously, you can directly insert entries into the RequestChan.

Lifecycle:

  • The event loop is started up by calling the Run() function, which will allocate some resources and then start up a background Goroutine.
  • You need to stop the event loop to remove the Goroutine and to free up any resources that have been allocated in the Run() function.

func NewRasterizer

func NewRasterizer(filename string, rasterBufferSize int) *Rasterizer

NewRasterizer returns a configured Rasterizer for a given filename, which uses a buffered channel of rasterBufferSize to queue requests for the rasterizer.

func (*Rasterizer) GeneratePageImage

func (r *Rasterizer) GeneratePageImage(ctx context.Context, pageNumber int, width int, scale float64) (image.Image, error)

GeneratePageImage is a synchronous interface to the processing engine and will return a Go stdlib image.Image.

func (*Rasterizer) GeneratePageSVG

func (r *Rasterizer) GeneratePageSVG(ctx context.Context, pageNumber int, width int, scale float64) ([]byte, error)

GeneratePageSVG is a synchronous interface to the processing engine and will return a Go byte array containing the SVG string.

func (*Rasterizer) GetPageCount

func (r *Rasterizer) GetPageCount() int

GetPageCount returns the number of pages in the document

func (*Rasterizer) Run

func (r *Rasterizer) Run() error

Run starts the main even loop after allocating some resources. This needs to be called before making any requests to rasterize pages.

func (*Rasterizer) Stop

func (r *Rasterizer) Stop()

Stop shuts down the rasterizer and frees up some common data structures that were allocated in the Run() method.

type ReplyWrapper

type ReplyWrapper interface {
	Error() error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL