Documentation
¶
Overview ¶
Package pdf provides a PDF viewer widget for Fyne, written in pure Go.
Document is a ready-to-use viewer with a thumbnail strip and zoom controls. Source gives goroutine-safe low-level access to an opened PDF (page count, bounds, rendering, metadata), and NewImageFromMemory renders a single page without any UI.
Page indices are 0-based throughout the package.
Index ¶
- Constants
- func NewImageFromMemory(contents []byte, pageNumber int, documentCallbacks ...DocumentCallbackFn) (image.Image, error)
- type Document
- func (d *Document) Close() error
- func (d *Document) CreateRenderer() fyne.WidgetRenderer
- func (d *Document) CurrentPage() int
- func (d *Document) LoadFromMemory(contents []byte) error
- func (d *Document) PageCount() int
- func (d *Document) Refresh()
- func (d *Document) Resize(size fyne.Size)
- func (d *Document) ShowPage(page int)
- func (d *Document) Zoom(scale float64)
- func (d *Document) ZoomToFit()
- func (d *Document) ZoomToFitHorizontal()
- func (d *Document) ZoomToFitVertical()
- type DocumentCallbackFn
- type Page
- type Source
- func (s *Source) Bound(page int) (image.Rectangle, error)
- func (s *Source) Close() error
- func (s *Source) Metadata() map[string]string
- func (s *Source) PageCount() int
- func (s *Source) RenderPage(page int, dpi float64) (image.Image, error)
- func (s *Source) RenderPageContext(ctx context.Context, page int, dpi float64) (image.Image, error)
- type Thumbnail
- func (t *Thumbnail) CreateRenderer() fyne.WidgetRenderer
- func (t *Thumbnail) Selected() bool
- func (t *Thumbnail) SetImage(theImage image.Image)
- func (t *Thumbnail) SetMinimumSize(size fyne.Size)
- func (t *Thumbnail) SetSelected(selected bool)
- func (t *Thumbnail) SetTitle(title string)
- func (t *Thumbnail) Tapped(_ *fyne.PointEvent)
Examples ¶
Constants ¶
const ThumbnailMinimumDimension = float32(100)
ThumbnailMinimumDimension is the default width and height of a thumbnail.
Variables ¶
This section is empty.
Functions ¶
func NewImageFromMemory ¶
func NewImageFromMemory(contents []byte, pageNumber int, documentCallbacks ...DocumentCallbackFn) (image.Image, error)
NewImageFromMemory renders page pageNumber (0-based) of the PDF in contents at 144 dpi. documentCallbacks are called with the opened Source before rendering.
Example ¶
package main
import (
"fmt"
"os"
pdf "github.com/timzifer/fyne-pdf"
)
func main() {
contents, err := os.ReadFile("example.pdf")
if err != nil {
panic(err)
}
img, err := pdf.NewImageFromMemory(contents, 0)
if err != nil {
panic(err)
}
fmt.Println(img.Bounds().Size())
}
Output:
Types ¶
type Document ¶
type Document struct {
widget.BaseWidget
// BackgroundColor fills the area behind the page.
BackgroundColor color.Color
// SaveCallback, if set, shows a save button in the toolbar that calls
// it. Call Refresh after changing it.
SaveCallback func()
// OnError is called on the main goroutine when a page cannot be
// rendered. If nil, the error is logged.
OnError func(page int, err error)
// contains filtered or unexported fields
}
Document is a PDF viewer widget: a page view with a toolbar for zooming and a thumbnail strip for navigation. Create it with NewDocument.
Pages are rendered in the background; the resolution of the main view follows the zoom level. Methods must be called from the Fyne main goroutine (or before the app runs).
func NewDocument ¶
func NewDocument() *Document
NewDocument creates an empty viewer. Load a PDF with Document.LoadFromMemory.
func (*Document) Close ¶
Close releases the loaded PDF and stops pending renders. Pages already shown stay visible.
func (*Document) CreateRenderer ¶
func (d *Document) CreateRenderer() fyne.WidgetRenderer
func (*Document) CurrentPage ¶
CurrentPage returns the index (0-based) of the page shown in the main view.
func (*Document) LoadFromMemory ¶
LoadFromMemory opens the PDF in contents and shows its first page. A previously loaded PDF is closed. Pages are rendered in the background.
func (*Document) Resize ¶
Resize lays out the widget and applies a ZoomToFit that was requested before the widget had a size.
func (*Document) Zoom ¶
Zoom sets the zoom factor; 1 shows the page at roughly its physical size. The factor is clamped to 0.2 to 4.
func (*Document) ZoomToFit ¶
func (d *Document) ZoomToFit()
ZoomToFit scales the page so it fits completely into the view.
func (*Document) ZoomToFitHorizontal ¶
func (d *Document) ZoomToFitHorizontal()
ZoomToFitHorizontal scales the page to the width of the view.
func (*Document) ZoomToFitVertical ¶
func (d *Document) ZoomToFitVertical()
ZoomToFitVertical scales the page to the height of the view.
type DocumentCallbackFn ¶
type DocumentCallbackFn func(src *Source)
DocumentCallbackFn is called with the opened Source before rendering, see NewImageFromMemory.
type Page ¶
type Page struct {
widget.BaseWidget
Scale float64
BackgroundColor color.Color
// contains filtered or unexported fields
}
Page displays a single rendered page image. Its minimum size is Page.PageSize multiplied by Scale; the image is scaled to fit, so it can be rendered at any resolution. The aspect ratio is kept.
func NewPageWithImage ¶
NewPageWithImage creates a Page showing img.
func (*Page) CreateRenderer ¶
func (p *Page) CreateRenderer() fyne.WidgetRenderer
func (*Page) PageSize ¶
PageSize returns the size of the page at Scale 1. Unless set with Page.SetPageSize, this is the size of the page image in pixels.
func (*Page) SetPageSize ¶
SetPageSize sets the size of the page at Scale 1, independent of the resolution of the image.
type Source ¶
type Source struct {
// contains filtered or unexported fields
}
Source is an opened PDF. All methods are safe for concurrent use: cera only allows interpreting pages of a document from one goroutine at a time, hence the mutex. A single page is still rasterized by cera on all cores.
func OpenSource ¶
OpenSource parses the PDF in contents.
Example ¶
package main
import (
"fmt"
"os"
pdf "github.com/timzifer/fyne-pdf"
)
func main() {
contents, err := os.ReadFile("example.pdf")
if err != nil {
panic(err)
}
src, err := pdf.OpenSource(contents)
if err != nil {
panic(err)
}
defer func() { _ = src.Close() }()
fmt.Println("pages:", src.PageCount())
fmt.Println("title:", src.Metadata()["title"])
img, err := src.RenderPage(0, 300)
if err != nil {
panic(err)
}
fmt.Println("size:", img.Bounds().Size())
}
Output:
func (*Source) Bound ¶
Bound returns the visible area of page (0-based) in points (72 dpi), with /Rotate and /UserUnit already applied.
func (*Source) Close ¶
Close releases the document. Afterwards all methods return an error or zero pages. Closing more than once is allowed.
func (*Source) Metadata ¶
Metadata returns the entries of the document info dictionary with lowercase keys ("title", "author", ...).
func (*Source) RenderPage ¶
RenderPage renders page (0-based) at the given resolution onto a white background. If rendering takes longer than 15 seconds, the partially drawn image is returned without an error.
type Thumbnail ¶
type Thumbnail struct {
widget.BaseWidget
OnTapped func()
// contains filtered or unexported fields
}
Thumbnail is a tappable page preview with a title below it.
func (*Thumbnail) CreateRenderer ¶
func (t *Thumbnail) CreateRenderer() fyne.WidgetRenderer
func (*Thumbnail) SetMinimumSize ¶
SetMinimumSize sets the size of the preview image area.
func (*Thumbnail) SetSelected ¶
SetSelected highlights the thumbnail, e.g. for the page currently shown.
func (*Thumbnail) Tapped ¶
func (t *Thumbnail) Tapped(_ *fyne.PointEvent)