lazypdf

package module
v0.0.0-...-07e6e40 Latest Latest
Warning

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

Go to latest
Published: May 20, 2021 License: AGPL-3.0 Imports: 10 Imported by: 3

README

LazyPDF

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

The only requirement is to build first MuPDF with the ./build script.

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
	Logger     zerolog.Logger
	// 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
	Logger      zerolog.Logger
	// 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 NewRasterizerWithLogger

func NewRasterizerWithLogger(filename string, rasterBufferSize int, logger zerolog.Logger) *Rasterizer

NewRasterizerWithLogger same as NewRasterizer but this function also accepts a logger.

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