gopdf

package module
v0.0.0-...-ddb0baf Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2015 License: MIT Imports: 16 Imported by: 0

README

gopdf

gopdf is a simple library for generating PDF document written in Go lang.

####Changelogs

2015-08-07

  • Add support for Unicode subfont embedding. (Chinese, Korean and Japanese fonts are now supported.)
  • No longer need to create font maps.

##Installation

go get -u github.com/signintech/gopdf

##Sample code

package main
import (
  "fmt"
  "github.com/signintech/gopdf"
)

func main() {

  pdf := gopdf.GoPdf{}
  pdf.Start(gopdf.Config{Unit: "pt", PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  pdf.AddPage()
  err := pdf.AddTTFFont("HDZB_5", "../ttf/wts11.ttf")
  if err != nil {
      log.Print(err.Error())
      return
  }
  
  err = pdf.SetFont("HDZB_5", "", 14)
  if err != nil {
      log.Print(err.Error())
      return
  }
  pdf.Cell(nil, "您好")
  pdf.WritePdf("hello.pdf")

}

visit https://github.com/oneplus1000/gopdfsample for more samples.

Documentation

Index

Constants

View Source
const ARG_1_AND_2_ARE_WORDS = 1
View Source
const CURRENT_FONT_TYPE_IFONT = 0
View Source
const CURRENT_FONT_TYPE_SUBSET = 1
View Source
const MORE_COMPONENTS = 32
View Source
const WE_HAVE_AN_X_AND_Y_SCALE = 64
View Source
const WE_HAVE_A_SCALE = 8
View Source
const WE_HAVE_A_TWO_BY_TWO = 128

Variables

View Source
var EntrySelectors = []int{
	0, 0, 1, 1, 2, 2,
	2, 2, 3, 3, 3, 3,
	3, 3, 3, 3, 4, 4,
	4, 4, 4, 4, 4, 4,
	4, 4, 4, 4, 4, 4, 4,
}
View Source
var ErrCharNotFound = errors.New("char not found")
View Source
var ErrNotSupportShortIndexYet = errors.New("not suport none short index yet!")

Functions

func CheckSum

func CheckSum(data []byte) uint64

func Chr

func Chr(n int) byte

func ContentObj_CalTextHeight

func ContentObj_CalTextHeight(fontsize int) float64

cal text height

func CreateEmbeddedFontSubsetName

func CreateEmbeddedFontSubsetName(name string) string

func DebugSubType

func DebugSubType(b []byte)

func DesignUnitsToPdf

func DesignUnitsToPdf(val int64, unitsPerEm uint64) int64

func FontConvertHelper_Cw2Str

func FontConvertHelper_Cw2Str(cw FontCw) string

func ReadShortFromByte

func ReadShortFromByte(data []byte, offset int) (int64, int)

func ReadUShortFromByte

func ReadUShortFromByte(data []byte, offset int) (uint64, int)

func StrHelperGetStringWidth

func StrHelperGetStringWidth(str string, fontSize int, ifont IFont) float64

func ToByte

func ToByte(chr string) byte

func WriteBytes

func WriteBytes(w io.Writer, data []byte, offset int, count int) error

func WriteTag

func WriteTag(w io.Writer, tag string) error

func WriteUInt16

func WriteUInt16(w io.Writer, v uint) error

func WriteUInt32

func WriteUInt32(w io.Writer, v uint) error

Types

type BasicObj

type BasicObj struct {
	Data string
	// contains filtered or unexported fields
}

BasicObj : basic object in pdf

func (*BasicObj) Build

func (b *BasicObj) Build() error

Build : build buff

func (*BasicObj) GetObjBuff

func (b *BasicObj) GetObjBuff() *bytes.Buffer

GetObjBuff : get buffer

func (*BasicObj) GetType

func (b *BasicObj) GetType() string

GetType : type of object

func (*BasicObj) Init

func (b *BasicObj) Init(funcGetRoot func() *GoPdf)

Init : init BasicObj

type Buff

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

Buff

func (*Buff) Bytes

func (b *Buff) Bytes() []byte

Bytes : get bytes

func (*Buff) Len

func (b *Buff) Len() int

Len : len of buffer

func (*Buff) Position

func (b *Buff) Position() int

Position : get current postion

func (*Buff) SetPosition

func (b *Buff) SetPosition(pos int)

SetPosition : set current postion

func (*Buff) Write

func (b *Buff) Write(p []byte) (int, error)

Write : write []byte to buffer

type CIDFontObj

type CIDFontObj struct {
	PtrToSubsetFontObj *SubsetFontObj
	// contains filtered or unexported fields
}

func (*CIDFontObj) Build

func (me *CIDFontObj) Build() error

func (*CIDFontObj) GetObjBuff

func (me *CIDFontObj) GetObjBuff() *bytes.Buffer

func (*CIDFontObj) GetType

func (me *CIDFontObj) GetType() string

func (*CIDFontObj) Init

func (me *CIDFontObj) Init(funcGetRoot func() *GoPdf)

func (*CIDFontObj) SetIndexObjSubfontDescriptor

func (me *CIDFontObj) SetIndexObjSubfontDescriptor(index int)

func (*CIDFontObj) SetPtrToSubsetFontObj

func (me *CIDFontObj) SetPtrToSubsetFontObj(ptr *SubsetFontObj)

type CatalogObj

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

func (*CatalogObj) Build

func (me *CatalogObj) Build() error

func (*CatalogObj) GetObjBuff

func (me *CatalogObj) GetObjBuff() *bytes.Buffer

func (*CatalogObj) GetType

func (me *CatalogObj) GetType() string

func (*CatalogObj) Init

func (me *CatalogObj) Init(funcGetRoot func() *GoPdf)

type Config

type Config struct {
	//pt , mm , cm , in
	Unit     string
	PageSize Rect
	K        float64
}

type ContentObj

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

func (*ContentObj) AppendStream

func (c *ContentObj) AppendStream(rectangle *Rect, text string)

func (*ContentObj) AppendStreamImage

func (c *ContentObj) AppendStreamImage(index int, x float64, y float64, rect *Rect)

func (*ContentObj) AppendStreamLine

func (c *ContentObj) AppendStreamLine(x1 float64, y1 float64, x2 float64, y2 float64)

func (*ContentObj) AppendStreamOval

func (c *ContentObj) AppendStreamOval(x1 float64, y1 float64, x2 float64, y2 float64)

func (*ContentObj) AppendStreamSetGrayFill

func (c *ContentObj) AppendStreamSetGrayFill(w float64)

Set the grayscale fills

func (*ContentObj) AppendStreamSetGrayStroke

func (c *ContentObj) AppendStreamSetGrayStroke(w float64)

Set the grayscale stroke

func (*ContentObj) AppendStreamSetLineWidth

func (c *ContentObj) AppendStreamSetLineWidth(w float64)

func (*ContentObj) AppendStreamSubsetFont

func (c *ContentObj) AppendStreamSubsetFont(rectangle *Rect, text string)

func (*ContentObj) AppendUnderline

func (c *ContentObj) AppendUnderline(startX float64, y float64, endX float64, endY float64, text string)

func (*ContentObj) Build

func (c *ContentObj) Build() error

func (*ContentObj) GetObjBuff

func (c *ContentObj) GetObjBuff() *bytes.Buffer

func (*ContentObj) GetType

func (c *ContentObj) GetType() string

func (*ContentObj) Init

func (c *ContentObj) Init(funcGetRoot func() *GoPdf)

type Current

type Current struct {
	X float64
	Y float64

	//font
	IndexOfFontObj int
	CountOfFont    int
	CountOfL       int

	Font_Size      int
	Font_Style     string
	Font_FontCount int
	Font_Type      int // CURRENT_FONT_TYPE_IFONT or  CURRENT_FONT_TYPE_SUBSET

	Font_IFont   IFont   // depend on Font_Type
	Font_ISubset ISubset // depend on Font_Type

	//page
	IndexOfPageObj int

	//img
	CountOfImg int
	//cache of image in pdf file
	ImgCaches []ImageCache
	// contains filtered or unexported fields
}

type EmbedFontObj

type EmbedFontObj struct {
	Data string
	// contains filtered or unexported fields
}

func (*EmbedFontObj) Build

func (e *EmbedFontObj) Build() error

func (*EmbedFontObj) GetObjBuff

func (e *EmbedFontObj) GetObjBuff() *bytes.Buffer

func (*EmbedFontObj) GetType

func (e *EmbedFontObj) GetType() string

func (*EmbedFontObj) Init

func (e *EmbedFontObj) Init(funcGetRoot func() *GoPdf)

func (*EmbedFontObj) SetFont

func (e *EmbedFontObj) SetFont(font IFont, zfontpath string)

type EncodingObj

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

func (*EncodingObj) Build

func (e *EncodingObj) Build() error

func (*EncodingObj) GetFont

func (e *EncodingObj) GetFont() IFont

func (*EncodingObj) GetObjBuff

func (e *EncodingObj) GetObjBuff() *bytes.Buffer

func (*EncodingObj) GetType

func (e *EncodingObj) GetType() string

func (*EncodingObj) Init

func (e *EncodingObj) Init(funcGetRoot func() *GoPdf)

func (*EncodingObj) SetFont

func (e *EncodingObj) SetFont(font IFont)

type FontCw

type FontCw map[byte]int

type FontDescItem

type FontDescItem struct {
	Key string
	Val string
}

type FontDescriptorObj

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

func (*FontDescriptorObj) Build

func (f *FontDescriptorObj) Build() error

func (*FontDescriptorObj) GetFont

func (f *FontDescriptorObj) GetFont() IFont

func (*FontDescriptorObj) GetObjBuff

func (f *FontDescriptorObj) GetObjBuff() *bytes.Buffer

func (*FontDescriptorObj) GetType

func (f *FontDescriptorObj) GetType() string

func (*FontDescriptorObj) Init

func (f *FontDescriptorObj) Init(funcGetRoot func() *GoPdf)

func (*FontDescriptorObj) SetFont

func (f *FontDescriptorObj) SetFont(font IFont)

func (*FontDescriptorObj) SetFontFileObjRelate

func (f *FontDescriptorObj) SetFontFileObjRelate(relate string)

type FontObj

type FontObj struct {
	Family string
	//Style string
	//Size int
	IsEmbedFont bool

	Font        IFont
	CountOfFont int
	// contains filtered or unexported fields
}

func (*FontObj) Build

func (f *FontObj) Build() error

func (*FontObj) GetObjBuff

func (f *FontObj) GetObjBuff() *bytes.Buffer

func (*FontObj) GetType

func (f *FontObj) GetType() string

func (*FontObj) Init

func (f *FontObj) Init(funcGetRoot func() *GoPdf)

func (*FontObj) SetIndexObjEncoding

func (f *FontObj) SetIndexObjEncoding(index int)

func (*FontObj) SetIndexObjFontDescriptor

func (f *FontObj) SetIndexObjFontDescriptor(index int)

func (*FontObj) SetIndexObjWidth

func (f *FontObj) SetIndexObjWidth(index int)

type GoPdf

type GoPdf struct {

	//ต่ำแหน่งปัจจุบัน
	Curr Current
	// contains filtered or unexported fields
}

GoPdf : A simple library for generating PDF written in Go lang

func (*GoPdf) AddFont

func (gp *GoPdf) AddFont(family string, ifont IFont, zfontpath string)

AddFont : user embed font in zfont file ( deprecated remove sogp day )

func (*GoPdf) AddPage

func (gp *GoPdf) AddPage()

AddPage : add new page

func (*GoPdf) AddTTFFont

func (gp *GoPdf) AddTTFFont(family string, ttfpath string) error

AddTTFFont : font use subtype font

func (*GoPdf) Br

func (gp *GoPdf) Br(h float64)

Br : new line

func (*GoPdf) Cell

func (gp *GoPdf) Cell(rectangle *Rect, text string)

Cell : create cell of text Note that this has no effect on Rect.H pdf (now). Fix later :-)

func (*GoPdf) GetBytesPdf

func (gp *GoPdf) GetBytesPdf() []byte

GetBytesPdf : get bytes of pdf file

func (*GoPdf) GetBytesPdfReturnErr

func (gp *GoPdf) GetBytesPdfReturnErr() ([]byte, error)

GetBytesPdfReturnErr : get bytes of pdf file

func (*GoPdf) GetX

func (gp *GoPdf) GetX() float64

GetX : get current position X

func (*GoPdf) GetY

func (gp *GoPdf) GetY() float64

GetY : get current position y

func (*GoPdf) Image

func (gp *GoPdf) Image(picPath string, x float64, y float64, rect *Rect)

Image : draw image

func (*GoPdf) Line

func (gp *GoPdf) Line(x1 float64, y1 float64, x2 float64, y2 float64)

Line : draw line

func (*GoPdf) Oval

func (gp *GoPdf) Oval(x1 float64, y1 float64, x2 float64, y2 float64)

Oval : draw oval

func (*GoPdf) SetFont

func (gp *GoPdf) SetFont(family string, style string, size int) error

SetFont : set font style support "" or "U"

func (*GoPdf) SetGrayFill

func (gp *GoPdf) SetGrayFill(grayScale float64)

Set the grayscale for the fill, takes a float64 between 0.0 and 1.0

func (*GoPdf) SetGrayStroke

func (gp *GoPdf) SetGrayStroke(grayScale float64)

Set the grayscale for the stroke, takes a float64 between 0.0 and 1.0

func (*GoPdf) SetLeftMargin

func (gp *GoPdf) SetLeftMargin(margin float64)

SetLeftMargin : set left margin

func (*GoPdf) SetLineWidth

func (gp *GoPdf) SetLineWidth(width float64)

SetLineWidth : set line width

func (*GoPdf) SetTextColor

func (gp *GoPdf) SetTextColor(r uint8, g uint8, b uint8)

SetTextColor : function sets the text color

func (*GoPdf) SetTopMargin

func (gp *GoPdf) SetTopMargin(margin float64)

SetTopMargin : set top margin

func (*GoPdf) SetX

func (gp *GoPdf) SetX(x float64)

SetX : set current position X

func (*GoPdf) SetY

func (gp *GoPdf) SetY(y float64)

SetY : set current position y

func (*GoPdf) Start

func (gp *GoPdf) Start(config Config)

Start : init gopdf

func (*GoPdf) WritePdf

func (gp *GoPdf) WritePdf(pdfPath string)

WritePdf : wirte pdf file

type IFont

type IFont interface {
	Init()
	GetType() string
	GetName() string
	GetDesc() []FontDescItem
	GetUp() int
	GetUt() int
	GetCw() FontCw
	GetEnc() string
	GetDiff() string
	GetOriginalsize() int

	SetFamily(family string)
	GetFamily() string
}

type IObj

type IObj interface {
	Init(func() *GoPdf)
	GetType() string
	GetObjBuff() *bytes.Buffer
	//สร้าง ข้อมูลใน pdf
	Build() error
}

type ISubset

type ISubset interface {
	AddChars(txt string)
	CharIndex(r rune) (uint64, error) //get char index
	CharWidth(r rune) (uint64, error) //find chear width
	GetUt() int64
}

type ImageCache

type ImageCache struct {
	Path  string
	Index int
}

type ImageObj

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

func (*ImageObj) Build

func (i *ImageObj) Build() error

func (*ImageObj) GetObjBuff

func (i *ImageObj) GetObjBuff() *bytes.Buffer

func (*ImageObj) GetRect

func (i *ImageObj) GetRect() *Rect

func (*ImageObj) GetType

func (i *ImageObj) GetType() string

func (*ImageObj) Init

func (i *ImageObj) Init(funcGetRoot func() *GoPdf)

func (*ImageObj) SetImagePath

func (i *ImageObj) SetImagePath(path string)

type PageObj

type PageObj struct {
	Contents        string
	ResourcesRelate string
	// contains filtered or unexported fields
}

func (*PageObj) Build

func (p *PageObj) Build() error

func (*PageObj) GetObjBuff

func (p *PageObj) GetObjBuff() *bytes.Buffer

func (*PageObj) GetType

func (p *PageObj) GetType() string

func (*PageObj) Init

func (p *PageObj) Init(funcGetRoot func() *GoPdf)

type PagesObj

type PagesObj struct {
	PageCount int
	Kids      string
	// contains filtered or unexported fields
}

func (*PagesObj) Build

func (p *PagesObj) Build() error

func (*PagesObj) GetObjBuff

func (p *PagesObj) GetObjBuff() *bytes.Buffer

func (*PagesObj) GetType

func (p *PagesObj) GetType() string

func (*PagesObj) Init

func (p *PagesObj) Init(funcGetRoot func() *GoPdf)

func (*PagesObj) Test

func (p *PagesObj) Test()

type PdfDictionaryObj

type PdfDictionaryObj struct {
	PtrToSubsetFontObj *SubsetFontObj
	// contains filtered or unexported fields
}

func (*PdfDictionaryObj) AddCompositeGlyphs

func (p *PdfDictionaryObj) AddCompositeGlyphs(glyphArray *[]int, glyph int)

add composite glyph composite glyph is a Unicode entity that can be defined as a sequence of one or more other characters.

func (*PdfDictionaryObj) Build

func (p *PdfDictionaryObj) Build() error

func (*PdfDictionaryObj) GetObjBuff

func (p *PdfDictionaryObj) GetObjBuff() *bytes.Buffer

func (*PdfDictionaryObj) GetOffset

func (p *PdfDictionaryObj) GetOffset(glyph int) int

func (*PdfDictionaryObj) GetType

func (p *PdfDictionaryObj) GetType() string

func (*PdfDictionaryObj) Init

func (p *PdfDictionaryObj) Init(funcGetRoot func() *GoPdf)

func (*PdfDictionaryObj) SetPtrToSubsetFontObj

func (p *PdfDictionaryObj) SetPtrToSubsetFontObj(ptr *SubsetFontObj)

type ProcSetObj

type ProcSetObj struct {

	//Font
	Realtes     RelateFonts
	RealteXobjs RealteXobjects
	// contains filtered or unexported fields
}

func (*ProcSetObj) Build

func (me *ProcSetObj) Build() error

func (*ProcSetObj) GetObjBuff

func (me *ProcSetObj) GetObjBuff() *bytes.Buffer

func (*ProcSetObj) GetType

func (me *ProcSetObj) GetType() string

func (*ProcSetObj) Init

func (me *ProcSetObj) Init(funcGetRoot func() *GoPdf)

type RealteXobject

type RealteXobject struct {
	IndexOfObj int
}

type RealteXobjects

type RealteXobjects []RealteXobject

type Rect

type Rect struct {
	W float64
	H float64
}

type RelateFont

type RelateFont struct {
	Family string
	//etc /F1
	CountOfFont int
	//etc  5 0 R
	IndexOfObj int
}

type RelateFonts

type RelateFonts []RelateFont

func (*RelateFonts) IsContainsFamily

func (me *RelateFonts) IsContainsFamily(family string) bool

type Rgb

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

type SubfontDescriptorObj

type SubfontDescriptorObj struct {
	PtrToSubsetFontObj *SubsetFontObj
	// contains filtered or unexported fields
}

func (*SubfontDescriptorObj) Build

func (s *SubfontDescriptorObj) Build() error

func (*SubfontDescriptorObj) GetObjBuff

func (s *SubfontDescriptorObj) GetObjBuff() *bytes.Buffer

func (*SubfontDescriptorObj) GetType

func (s *SubfontDescriptorObj) GetType() string

func (*SubfontDescriptorObj) Init

func (s *SubfontDescriptorObj) Init(func() *GoPdf)

func (*SubfontDescriptorObj) SetIndexObjPdfDictionary

func (s *SubfontDescriptorObj) SetIndexObjPdfDictionary(index int)

func (*SubfontDescriptorObj) SetPtrToSubsetFontObj

func (s *SubfontDescriptorObj) SetPtrToSubsetFontObj(ptr *SubsetFontObj)

type SubsetFontObj

type SubsetFontObj struct {
	Family                string
	CharacterToGlyphIndex map[rune]uint64
	CountOfFont           int
	// contains filtered or unexported fields
}

PdfType0 Font

func (*SubsetFontObj) AddChars

func (s *SubsetFontObj) AddChars(txt string)

func (*SubsetFontObj) Build

func (s *SubsetFontObj) Build() error

func (*SubsetFontObj) CharCodeToGlyphIndex

func (s *SubsetFontObj) CharCodeToGlyphIndex(r rune) uint64

func (*SubsetFontObj) CharIndex

func (s *SubsetFontObj) CharIndex(r rune) (uint64, error)

func (*SubsetFontObj) CharWidth

func (s *SubsetFontObj) CharWidth(r rune) (uint64, error)

func (*SubsetFontObj) GetFamily

func (s *SubsetFontObj) GetFamily() string

func (*SubsetFontObj) GetObjBuff

func (s *SubsetFontObj) GetObjBuff() *bytes.Buffer

func (*SubsetFontObj) GetTTFParser

func (s *SubsetFontObj) GetTTFParser() *core.TTFParser

func (*SubsetFontObj) GetType

func (s *SubsetFontObj) GetType() string

func (*SubsetFontObj) GetUt

func (s *SubsetFontObj) GetUt() int64

func (*SubsetFontObj) GlyphIndexToPdfWidth

func (s *SubsetFontObj) GlyphIndexToPdfWidth(glyphIndex uint64) uint64

func (*SubsetFontObj) Init

func (s *SubsetFontObj) Init(funcGetRoot func() *GoPdf)

func (*SubsetFontObj) SetFamily

func (s *SubsetFontObj) SetFamily(familyname string)

func (*SubsetFontObj) SetIndexObjCIDFont

func (s *SubsetFontObj) SetIndexObjCIDFont(index int)

func (*SubsetFontObj) SetIndexObjUnicodeMap

func (s *SubsetFontObj) SetIndexObjUnicodeMap(index int)

func (*SubsetFontObj) SetTTFByPath

func (s *SubsetFontObj) SetTTFByPath(ttfpath string) error

type UnicodeMap

type UnicodeMap struct {
	PtrToSubsetFontObj *SubsetFontObj
	// contains filtered or unexported fields
}

func (*UnicodeMap) Build

func (u *UnicodeMap) Build() error

func (*UnicodeMap) GetObjBuff

func (u *UnicodeMap) GetObjBuff() *bytes.Buffer

func (*UnicodeMap) GetType

func (u *UnicodeMap) GetType() string

func (*UnicodeMap) Init

func (u *UnicodeMap) Init(funcGetRoot func() *GoPdf)

func (*UnicodeMap) SetPtrToSubsetFontObj

func (u *UnicodeMap) SetPtrToSubsetFontObj(ptr *SubsetFontObj)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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