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 ¶
- Variables
- type Box
- type Document
- func (d *Document) Close() error
- func (d *Document) Describe(context.Context) (info ivy.Info, err error)
- func (d *Document) Page(ctx context.Context, n int) (ivy.Page, error)
- func (d *Document) PageCount(context.Context) (int, error)
- func (d *Document) Previews(ctx context.Context) iter.Seq2[ivy.Preview, error]
- type Option
- type Page
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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) Describe ¶
Describe satisfies the ivy.Describer interface. It reads the document information dictionary, and the language of the catalog.
type Option ¶
type Option func(*options)
Option is an option of Open.
func WithAnnotations ¶
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 ¶
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 ¶
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 ¶
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.