pdf

package
v0.0.0-...-786d6a2 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package pdf implements a minimal PDF 1.7 writer, just functional enough to create a PDF file containing multiple CCITT fax-encoded DIN A4 sized pages.

It follows the standard “PDF 32000-1:2008 PDF 1.7”: https://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf

Example
package main

import (
	"bytes"
	"fmt"
	"image"
	"log"
	"os"
	"time"

	"github.com/stapelberg/scan2drive/internal/pdf"
)

func main() {
	doc := &pdf.Catalog{
		Common: pdf.Common{ObjectName: "catalog"},
		Pages: &pdf.Pages{
			Common: pdf.Common{ObjectName: "pages"},
			Kids: []pdf.Object{
				&pdf.Page{
					Common: pdf.Common{ObjectName: "page0"},
					Resources: []pdf.Object{
						&pdf.Image{
							Common: pdf.Common{
								ObjectName: "scan0",
								Stream:     []byte("ß"),
							},
							Bounds: image.Rect(0, 0, 4960, 7016),
						},
					},
					Parent: "pages",
					Contents: []pdf.Object{
						&pdf.Common{
							ObjectName: "content0",
							Stream:     []byte(fmt.Sprintf("q 595.28 0 0 841.89 0.00 0.00 cm /%s Do Q\n", "scan0")),
						},
					},
				},
			},
		},
	}
	info := &pdf.DocumentInfo{
		Common:       pdf.Common{ObjectName: "info"},
		CreationDate: time.Unix(1493650928, 0).UTC(),
		Producer:     "https://github.com/stapelberg/scan2drive",
	}
	var buf bytes.Buffer
	if err := pdf.NewEncoder(&buf).Encode(doc, info); err != nil {
		log.Fatal(err)
	}
	// Convert the binary data at the beginning of a PDF to a
	// printable sequence of bytes for the Go example test
	// infrastructure.
	printable := bytes.Replace(buf.Bytes(), []byte{0xe2, 0xe3, 0xcf, 0xd3}, []byte("<0xE2E3CFD3>"), 1)
	// Remove trailing whitespace from xref table for more convenient
	// editing of example_test.go.
	printable = bytes.Replace(printable, []byte("00000 n "), []byte("00000 n"), -1)
	printable = bytes.Replace(printable, []byte("65535 f "), []byte("65535 f"), -1)
	os.Stdout.Write(printable)

}
Output:

%PDF-1.0
%<0xE2E3CFD3>
1 0 obj
<<
  /Type /Catalog
  /Pages 2 0 R
>>
endobj
2 0 obj
<<
  /Kids [3 0 R]
  /Type /Pages
  /Count 1
>>
endobj
3 0 obj
<<
  /Resources <<
    /XObject <<
/scan0 4 0 R
    >>
  >>
  /Contents [5 0 R]
  /Parent 2 0 R
  /Type /Page
  /MediaBox [ 0 0 595.28 841.89 ]
>>
endobj
4 0 obj
<<
  /Subtype /Image
  /DecodeParms
  <<
    /K 0
    /EndOfBlock false
    /EndOfLine true
    /BlackIs1 false
    /Rows 7016
    /Columns 4960
  >>
  /Type /XObject
  /Width 4960
  /Filter /CCITTFaxDecode
  /Height 7016
  /Length 2
  /BitsPerComponent 1
  /ColorSpace /DeviceGray
>>
stream
ß
endstream
endobj
5 0 obj
<<
  /Length 45
>>
stream
q 595.28 0 0 841.89 0.00 0.00 cm /scan0 Do Q

endstream
endobj
6 0 obj
<<
  /CreationDate (D:20170501150208+00'00')
  /Producer (https://github.com/stapelberg/scan2drive)
>>
endobj
xref
0 7
0000000000 65535 f
0000000015 00000 n
0000000068 00000 n
0000000131 00000 n
0000000293 00000 n
0000000613 00000 n
0000000710 00000 n
trailer
<<
  /Root 1 0 R
  /Size 7
  /Info 6 0 R
>>
startxref
827
%%EOF

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Catalog

type Catalog struct {
	Common
	Pages Object // Pages
}

Catalog represents a PDF catalog object.

func (*Catalog) Encode

func (r *Catalog) Encode(w io.Writer, ids map[string]ObjectID) error

Encode implements Object.

func (*Catalog) Objects

func (r *Catalog) Objects() []Object

Objects implements Object.

type Common

type Common struct {
	ObjectName string
	ID         ObjectID
	Stream     []byte
}

Common represents a PDF object.

func (*Common) Encode

func (c *Common) Encode(w io.Writer, ids map[string]ObjectID) error

Encode implements Object.

func (*Common) Name

func (c *Common) Name() string

Name implements Object.

func (*Common) Objects

func (c *Common) Objects() []Object

Objects implements Object.

func (*Common) SetID

func (c *Common) SetID(id ObjectID)

SetID implements Object.

func (*Common) String

func (c *Common) String() string

String implements fmt.Stringer.

type DocumentInfo

type DocumentInfo struct {
	Common

	CreationDate time.Time
	Producer     string
}

DocumentInfo represents a PDF document information object.

func (*DocumentInfo) Encode

func (d *DocumentInfo) Encode(w io.Writer, ids map[string]ObjectID) error

Encode implements Object.

func (*DocumentInfo) Objects

func (d *DocumentInfo) Objects() []Object

Objects implements Object.

type Encoder

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

Encoder is a PDF writer.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a ready-to-use Encoder writing to w.

func (*Encoder) Encode

func (e *Encoder) Encode(r *Catalog, info *DocumentInfo) error

Encode writes the PDF file represented by the specified catalog.

type Image

type Image struct {
	Common

	Bounds image.Rectangle
}

Image represents a PDF image object containing a DIN A4-sized page scanned with 600dpi (i.e. into 4960x7016 pixels).

func (*Image) Encode

func (i *Image) Encode(w io.Writer, ids map[string]ObjectID) error

Encode implements Object.

func (*Image) Objects

func (i *Image) Objects() []Object

Objects implements Object.

type Object

type Object interface {
	// Objects returns all Objects which should be encoded into the
	// PDF file.
	Objects() []Object

	// Encode encodes the object into the PDF file w.
	Encode(w io.Writer, ids map[string]ObjectID) error

	// SetID updates the object id.
	SetID(id ObjectID)

	// Name returns the human-readable object name.
	Name() string

	fmt.Stringer
}

Object is implemented by all PDF objects.

type ObjectID

type ObjectID int

ObjectID is a PDF object id.

func (ObjectID) String

func (o ObjectID) String() string

String implements fmt.Stringer.

type Page

type Page struct {
	Common

	Resources []Object // Image
	Contents  []Object // Common (streams)

	// Parent contains the human-readable name of the parent object,
	// which will be translated into an object ID when encoding.
	Parent string
}

Page represents a PDF page object with size DIN A4

func (*Page) Encode

func (p *Page) Encode(w io.Writer, ids map[string]ObjectID) error

Encode implements Object.

func (*Page) Objects

func (p *Page) Objects() []Object

Objects implements Object.

type Pages

type Pages struct {
	Common
	Kids []Object // Page
}

Pages represents a PDF pages object

func (*Pages) Encode

func (p *Pages) Encode(w io.Writer, ids map[string]ObjectID) error

Encode implements Object.

func (*Pages) Objects

func (p *Pages) Objects() []Object

Objects implements Object.

Jump to

Keyboard shortcuts

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