pdf

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 24 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pdf opens PDF files, and renders their pages.

The package reads a file with a port of hayro-syntax, runs the content of each page with a port of hayro-interpret, and draws its marks with a port of hayro onto the display list of ivy (D10, D14). It draws paths, text with the fonts of the file or the standard fonts, images, shadings, tiling patterns, soft masks, blend modes and annotations that have an appearance. A file that a password protects opens with the password of ivy.Config (D12).

A page that the package draws only in part returns an *ivy.PartialError (D5).

Adobe Illustrator writes a PDF page next to its own data in a file that is compatible with PDF. The package opens such a file, with the extension ai, as a PDF file.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Format = ivy.Format{
	Name:  "pdf",
	Mimes: []string{"application/pdf", "application/illustrator"},
	Exts:  []string{"pdf", "ai"},
	Open: func(ctx context.Context, src ivy.Source, cfg ivy.Config) (ivy.Document, error) {
		return Open(ctx, src, WithConfig(cfg))
	},
}

Format describes the package for the registry of a program. It lists the extension ai and the mime type of Illustrator, because Illustrator writes files that are compatible with PDF.

Functions

This section is empty.

Types

type Box

type Box int

Box is a page box of a PDF page (ISO 32000-1, 14.11.2). The zero Box is CropBox.

const (
	// CropBox is the region that a viewer shows. A page without a CropBox
	// uses its MediaBox.
	CropBox Box = iota
	// MediaBox is the whole medium of the page, with the margin of a
	// printer, such as the crop marks.
	MediaBox
	// BleedBox is the region that a printer prints, with the bleed. A page
	// without a BleedBox uses its CropBox.
	BleedBox
	// TrimBox is the size of the finished page after the trim. A page
	// without a TrimBox uses its CropBox.
	TrimBox
	// ArtBox is the region of the meaningful content of the page. A page
	// without an ArtBox uses its CropBox.
	ArtBox
)

The page boxes.

type Document

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

Document is an open PDF file.

func Open

func Open(ctx context.Context, src ivy.Source, opts ...Option) (d *Document, err error)

Open opens a PDF file. It reads the whole file into memory. It returns an error that wraps ivy.ErrFormat if src is not a PDF file, one that wraps ivy.ErrLimit if src is larger than Limits.MaxMemory, and one that wraps ivy.ErrPassword if the file needs a password that the configuration does not give.

func (*Document) Close

func (d *Document) Close() error

Close satisfies the ivy.Document interface.

func (*Document) Describe

func (d *Document) Describe(context.Context) (info ivy.Info, err error)

Describe satisfies the ivy.Describer interface. It reads the document information dictionary, and the language of the catalog.

func (*Document) Page

func (d *Document) Page(ctx context.Context, n int) (ivy.Page, error)

Page satisfies the ivy.Pager interface.

func (*Document) PageCount

func (d *Document) PageCount(context.Context) (int, error)

PageCount satisfies the ivy.Pager interface.

func (*Document) Previews

func (d *Document) Previews(ctx context.Context) iter.Seq2[ivy.Preview, error]

Previews satisfies the ivy.Previewer interface. It lists the thumbnail of each page that has one, the entry Thumb of the page, in the order of the pages. A file without thumbnails has no previews, and a program renders a page instead.

type Option

type Option func(*options)

Option is an option of Open.

func WithAnnotations

func WithAnnotations(on bool) Option

WithAnnotations draws the annotations of each page. It is on by default. The package draws the normal appearance of an annotation. An annotation without an appearance stream does not draw.

func WithConfig

func WithConfig(cfg ivy.Config) Option

WithConfig sets the configuration that every format shares.

Example

A program opens a PDF file that a password protects. Without the password, Open returns ivy.ErrPassword. A program can also give PasswordFunc, to ask a person for the password.

package main

import (
	"bytes"
	"context"
	"errors"
	"fmt"
	"os"
	"path/filepath"

	"github.com/xo/ivy"
	"github.com/xo/ivy/pdf"
)

func main() {
	ctx := context.Background()
	buf, err := os.ReadFile(filepath.Join("testdata", "password_2.pdf"))
	if err != nil {
		fmt.Println(err)
		return
	}
	_, err = pdf.Open(ctx, bytes.NewReader(buf))
	fmt.Println(errors.Is(err, ivy.ErrPassword))
	d, err := pdf.Open(ctx, bytes.NewReader(buf), pdf.WithConfig(ivy.Config{Password: "samplefiles"}))
	if err != nil {
		fmt.Println(err)
		return
	}
	defer d.Close()
	n, err := d.PageCount(ctx)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(n)
}
Output:
true
3

func WithPageBox

func WithPageBox(box Box) Option

WithPageBox sets the page box that each page renders, its size and its clip (D31). The default is CropBox. A page box that a page does not have takes its default from the PDF specification, and a page box is cut to the MediaBox. The rotation of the page applies to each page box.

type Page

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

Page is a page of a PDF file.

func (*Page) Draw

func (p *Page) Draw(ctx context.Context, d display.Device, res float32) error

Draw satisfies the display.Drawer interface (D58). It draws the page as Render does, onto a target of res pixels for each point, and scales the target back to points on d. The image of a glyph of Type 3 keeps the pixels of the file, as KeepGlyphImages of render says.

func (*Page) Render

func (p *Page) Render(ctx context.Context, dst *okage.Pixmap, t okage.Transform) error

Render satisfies the ivy.Page interface. It draws the content of the page inside the page box of WithPageBox, by default the CropBox, rotated by the entry Rotate of the page.

func (*Page) Size

func (p *Page) Size() (float32, float32)

Size satisfies the ivy.Page interface. It returns the size of the page box of WithPageBox, by default the CropBox, in points, after the rotation of the page.

Directories

Path Synopsis
Package pdfenc writes the pages of a document as a PDF file, through the PDF writer of okage (D58).
Package pdfenc writes the pages of a document as a PDF file, through the PDF writer of okage (D58).

Jump to

Keyboard shortcuts

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