davepdf

package module
v0.0.0-...-462397f Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2020 License: MIT Imports: 8 Imported by: 0

README

davepdf

PDF generator library

Features
  • Creates PDF 1.4 documents
  • Less than 1000 lines of code (currently: 871)
  • Less than 100 lines of code per file
  • ASCII text support
  • Support for 14 fonts: Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic, Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique, Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique, Symbol, ZapfDingbats
  • JPEG image support
  • PDF output contains only ASCII characters and can be opened in Notepad (except when importing existing PDFs)
  • PDF output includes comments about each PDF drawing instruction
  • Basic shape support: circle, ellipse, rectangle, line, bezier curves
  • Import existing PDF page and print on new PDF page
Limitations
  • Unsupported - use at your own risk
  • No support for external fonts
  • No Unicode or UTF-8 support
Example
package main

import "github.com/phpdave11/davepdf"

func main() {
	pdf := davepdf.NewPdf()

	pdf.AddPage()

	pdf.SetFontFamily("Courier-Bold")
	pdf.SetFontSize(24)
	pdf.SetXY(180, 10)
	pdf.Text("phpdave11/davepdf")

	fonts := []string{
		"Times-Roman", 
		"Times-Bold", 
		"Times-Italic", 
		"Times-BoldItalic", 
		"Courier", 
		"Courier-Bold", 
		"Courier-Oblique", 
		"Courier-BoldOblique", 
		"Helvetica", 
		"Helvetica-Bold", 
		"Helvetica-Oblique", 
		"Helvetica-BoldOblique", 
		"Symbol", 
		"ZapfDingbats"}

	for i := 0; i < len(fonts); i++ {
		pdf.SetFontFamily(fonts[i])
		pdf.SetFontSize(20)
		pdf.SetXY(10, 80 + float64(i * 25))
		pdf.Text("The quick brown fox jumps over the lazy dog.")
	}

	image := pdf.NewJPEGImageFromFile("/Users/dave-barnes/Desktop/go-img/go.jpg")
	pdf.DrawImage(image, 178, 460, 256, 256)

    pdf.Write()
}

Output (rendered PDF): rendered pdf

Output (PDF drawing instructions):

BT                                                   % begin text
  /FONT6 24 Tf                                       % set font family and font size
  180.000000 758.000000 Td                           % set position to draw text
  (phpdave11/davepdf)Tj                              % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT7 20 Tf                                       % set font family and font size
  10.000000 692.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT8 20 Tf                                       % set font family and font size
  10.000000 667.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT9 20 Tf                                       % set font family and font size
  10.000000 642.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT10 20 Tf                                      % set font family and font size
  10.000000 617.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT11 20 Tf                                      % set font family and font size
  10.000000 592.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT6 20 Tf                                       % set font family and font size
  10.000000 567.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT12 20 Tf                                      % set font family and font size
  10.000000 542.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT13 20 Tf                                      % set font family and font size
  10.000000 517.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT14 20 Tf                                      % set font family and font size
  10.000000 492.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT15 20 Tf                                      % set font family and font size
  10.000000 467.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT16 20 Tf                                      % set font family and font size
  10.000000 442.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT17 20 Tf                                      % set font family and font size
  10.000000 417.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT18 20 Tf                                      % set font family and font size
  10.000000 392.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
BT                                                   % begin text
  /FONT19 20 Tf                                      % set font family and font size
  10.000000 367.000000 Td                            % set position to draw text
  (The quick brown fox jumps over the lazy dog.)Tj   % write text
ET                                                   % end text
q 256 0 0 256 178.00000 76.00000 cm /Image20 Do Q    % draw image

Documentation

Index

Constants

View Source
const (
	FunctionType0 FunctionType = 0
	FunctionType2              = 2
	FunctionType3              = 3
	FunctionType4              = 4
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CMYKColor

type CMYKColor struct {
	C int
	Y int
	M int
	K int
}

type Color

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

type ColorSpace

type ColorSpace uint
const (
	ColorSpaceCMYK ColorSpace = iota
	ColorSpaceRGB
)

type FunctionType

type FunctionType = int

type LineCap

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

type LineCapStyle

type LineCapStyle = uint

Line cap style With type assertions in Go 1.9, you can simply add = where you define the type. This tells the compiler that LineCapStyle is an alternate name for uint.

const (
	LineCapStyleButt LineCapStyle = iota
	LineCapStyleRound
	LineCapStyleProjectingSquare
)

type LineDash

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

type LineDashPattern

type LineDashPattern = uint

Line dash pattern

const (
	LineDashPatternMiter LineDashPattern = iota
	LineDashPatternRound
	LineDashPatternBevel
)

type LineJoin

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

type LineJoinStyle

type LineJoinStyle = uint

Line join style

const (
	LineJoinStyleMiter LineJoinStyle = iota
	LineJoinStyleRound
	LineJoinStyleBevel
)

type Pdf

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

func NewPdf

func NewPdf() *Pdf

func (*Pdf) AddPage

func (pdf *Pdf) AddPage() *PdfPage

func (*Pdf) AppendCurve

func (pdf *Pdf) AppendCurve(x1, y1, x2, y2, x3, y3 float64)

func (*Pdf) Circle

func (pdf *Pdf) Circle(x, y, r float64, style string)

func (*Pdf) DrawImage

func (pdf *Pdf) DrawImage(image *PdfImage, x, y float64, w, h int)

func (*Pdf) Ellipse

func (pdf *Pdf) Ellipse(x, y, rx, ry float64, style string)

func (*Pdf) FillPath

func (pdf *Pdf) FillPath()

func (*Pdf) ImportPage

func (pdf *Pdf) ImportPage(sourceFile string, pageno int, box string) int

func (*Pdf) NewFunction

func (pdf *Pdf) NewFunction() *PdfFunction

func (*Pdf) NewJPEGImageFromFile

func (pdf *Pdf) NewJPEGImageFromFile(filename string) *PdfImage

func (*Pdf) NewPath

func (pdf *Pdf) NewPath(x, y float64)

func (*Pdf) NewShading

func (pdf *Pdf) NewShading() *PdfShading

func (*Pdf) Rect

func (pdf *Pdf) Rect(x, y, w, h float64, mode string)

func (*Pdf) SetFillColorCMYK

func (pdf *Pdf) SetFillColorCMYK(c, m, y, k int)

func (*Pdf) SetFontFamily

func (pdf *Pdf) SetFontFamily(fontFamily string)

func (*Pdf) SetFontSize

func (pdf *Pdf) SetFontSize(fontSize int)

func (*Pdf) SetLineCapStyle

func (pdf *Pdf) SetLineCapStyle(s uint)

func (*Pdf) SetLineDashPattern

func (pdf *Pdf) SetLineDashPattern(array []uint, phase uint)

func (*Pdf) SetLineJoinStyle

func (pdf *Pdf) SetLineJoinStyle(s uint)

func (*Pdf) SetLineWidth

func (pdf *Pdf) SetLineWidth(w float64)

Line width

func (*Pdf) SetStrokeColorCMYK

func (pdf *Pdf) SetStrokeColorCMYK(c, m, y, k int)

func (*Pdf) SetXY

func (pdf *Pdf) SetXY(x, y float64)

func (*Pdf) StrokeAndFillPath

func (pdf *Pdf) StrokeAndFillPath()

func (*Pdf) StrokePath

func (pdf *Pdf) StrokePath()

func (*Pdf) Text

func (pdf *Pdf) Text(text string)

func (*Pdf) UseImportedTemplate

func (pdf *Pdf) UseImportedTemplate(tplid int, x, y, w, h float64)

func (*Pdf) Write

func (pdf *Pdf) Write()

type PdfCatalog

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

type PdfContents

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

type PdfFont

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

type PdfFunction

type PdfFunction struct {
	Type          FunctionType
	Domain        []float64
	Size          []int
	Encode        []float64
	BitsPerSample int
	Range         []float64
	Decode        []float64
	Bounds        []float64
	Functions     []*PdfFunction
	C0            []float64
	C1            []float64
	N             float64
	// contains filtered or unexported fields
}

type PdfImage

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

type PdfInstruction

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

type PdfInstructions

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

func (*PdfInstructions) String

func (ins *PdfInstructions) String() string

type PdfObject

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

type PdfPage

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

type PdfPageTree

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

type PdfResources

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

type PdfShading

type PdfShading struct {
	Type     ShadingType
	Coords   []float64
	Function *PdfFunction
	Extend   []bool
	// contains filtered or unexported fields
}

type PdfXrefTable

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

type RGBColor

type RGBColor struct {
	R int
	G int
	B int
}

type ShadingType

type ShadingType = int
const (
	ShadingType3 ShadingType = 3
)

Jump to

Keyboard shortcuts

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