A simple library for generating PDF written in Go lang
Branch: master
Clone or download
oneplus1000 Merge pull request #100 from stobita/master
 Add multiple lines Cell method with auto text wrapping
Latest commit eaeb261 Feb 17, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
fontmaker Use bytes.Equal(), remove CompareBytes() Apr 8, 2018
test/res resize img Jul 8, 2016
.gitignore edit .gitignore Jan 16, 2018
Changelog.md add export function May 3, 2016
LICENSE add LICENSE Jan 16, 2015
README.md update README.md Dec 5, 2018
buff.go fixed don't use generic names such as "me", "this", or "self" Jan 29, 2016
buff_write.go clean code for golint : change all func in IObj to private Jan 29, 2016
buff_write_test.go write info to pdf Nov 10, 2016
buffer_pool.go Reduce memory consumption Sep 25, 2017
cache_contact_color.go Reduce memory consumption Sep 25, 2017
cache_content_gray.go Reduce memory consumption Sep 25, 2017
cache_content_image.go Reduce memory consumption Sep 25, 2017
cache_content_line.go When Draw Line, No Need Close Path, so use S NOT s Nov 9, 2018
cache_content_line_type.go Reduce memory consumption Sep 25, 2017
cache_content_line_width.go Reduce memory consumption Sep 25, 2017
cache_content_oval.go Reduce memory consumption Sep 25, 2017
cache_content_rectangle.go Reduce memory consumption Sep 25, 2017
cache_content_rotate.go RotateReset Nov 25, 2018
cache_content_text.go fixed #93 Oct 31, 2018
cache_contnent_curve.go Reduce memory consumption Sep 25, 2017
catalog_obj.go Reduce memory consumption Sep 25, 2017
cell_option.go fix comment of celloption May 2, 2016
cid_font_obj.go Reduce memory consumption Sep 25, 2017
config.go change interface iCacheContent.toStream() (*bytes.Buffer, error) Sep 29, 2016
content_obj.go RotateReset Nov 25, 2018
current.go Add support for font styles with same family name Oct 18, 2017
debug.go ... Jul 26, 2015
debug_ch.go rename debug.go Jul 26, 2015
debug_th.go ... Jul 26, 2015
device_rgb_obj.go Reduce memory consumption Sep 25, 2017
embedfont_obj.go Reduce memory consumption Sep 25, 2017
encoding_obj.go Reduce memory consumption Sep 25, 2017
encryption_obj.go Reduce memory consumption Sep 25, 2017
font_obj.go Reduce memory consumption Sep 25, 2017
font_option.go Add support for font styles with same family name Oct 18, 2017
fontconverthelper.go int cid font Jun 13, 2015
fontdescriptor_obj.go Reduce memory consumption Sep 25, 2017
func_kern_override.go change pair value to int16 Apr 18, 2016
gopdf.go set x position Feb 10, 2019
gopdf_test.go fix error when png not embed profile Apr 26, 2018
i_cache_contneter.go Reduce memory consumption Sep 25, 2017
ifont.go go fmt Aug 8, 2015
image_holder.go add ImageHolderByReader Jun 24, 2017
image_obj.go Reduce memory consumption Sep 25, 2017
image_obj_parse.go fix error when png not embed profile Apr 26, 2018
image_obj_test.go fix error when png not embed profile Apr 26, 2018
img_info.go can show indexed png Jun 18, 2016
iobj.go Reduce memory consumption Sep 25, 2017
kern_test.go remove old code May 2, 2016
link_option.go Add support for internal/external links Mar 9, 2018
list_cache_content.go More bytes.Buffer removal Sep 25, 2017
map_of_character_To_glyph_index.go add MapOfCharacterToGlyphIndex.keyIndexs Aug 11, 2017
page_obj.go Add support for internal/external links Mar 9, 2018
page_option.go AddPageWithOption Jun 18, 2017
page_sizes.go Added pagesizes as readable variables Dec 4, 2018
pages_obj.go Reduce memory consumption Sep 25, 2017
pdf_dictionary_obj.go Reduce memory consumption Sep 25, 2017
pdf_info_obj.go add document information dictionary Nov 11, 2016
pdf_protection.go Provide a pdf.Write(io.Writer) method Sep 25, 2017
pdf_protection_test.go change interface iCacheContent.toStream() (*bytes.Buffer, error) Sep 29, 2016
procset_obj.go Merge branch 'writer' of https://github.com/CaptainCodeman/gopdf into… Oct 29, 2017
rect.go refactoring code Dec 16, 2013
smask_obj.go Reduce memory consumption Sep 25, 2017
strhelper.go fix space im font family name Jul 20, 2017
style.go rect with style and fill color Sep 20, 2016
subfont_descriptor_obj.go Reduce memory consumption Sep 25, 2017
subset_font_obj.go Merge branch 'writer' of https://github.com/CaptainCodeman/gopdf into… Oct 29, 2017
ttf_option.go Add support for font styles with same family name Oct 18, 2017
unicode_map.go Reduce memory consumption Sep 25, 2017

README.md

gopdf

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

Features

  • Unicode subfont embedding. (Chinese, Japanese, Korean, etc.)
  • Draw line, oval, rect, curve
  • Draw image ( jpg, png )
  • Password protection
  • Font kerning

Installation

go get -u github.com/signintech/gopdf

Print text

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

func main() {

	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{ PageSize: gopdf.PageSizeA4 })  
	pdf.AddPage()
	err := pdf.AddTTFFont("wts11", "../ttf/wts11.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}

	err = pdf.SetFont("wts11", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.Cell(nil, "您好")
	pdf.WritePdf("hello.pdf")

}

Image

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

func main() {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{PageSize: gopdf.PageSizeA4 })  
	pdf.AddPage()
	var err error
	err = pdf.AddTTFFont("loma", "../ttf/Loma.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}
	
	pdf.Image("../imgs/gopher.jpg", 200, 50, nil) //print image
	err = pdf.SetFont("loma", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.SetX(250) //move current location
	pdf.SetY(200)
	pdf.Cell(nil, "gopher and gopher") //print text

	pdf.WritePdf("image.pdf")
}
  

Links

package main

import (
	"log"
	"github.com/signintech/gopdf"
)

func main()  {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{ PageSize: gopdf.PageSizeA4 }) //595.28, 841.89 = A4
	pdf.AddPage()
	err := pdf.AddTTFFont("times", "./test/res/times.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}

	err = pdf.SetFont("times", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}

	pdf.SetX(30)
	pdf.SetY(40)
	pdf.Text("Link to example.com")
	pdf.AddExternalLink("http://example.com/", 27.5, 28, 125, 15)

	pdf.SetX(30)
	pdf.SetY(70)
	pdf.Text("Link to second page")
	pdf.AddInternalLink("anchor", 27.5, 58, 120, 15)

	pdf.AddPage()
	pdf.SetX(30)
	pdf.SetY(100)
	pdf.SetAnchor("anchor")
	pdf.Text("Anchor position")

	pdf.WritePdf("hello.tmp.pdf")

}

Draw line

pdf.SetLineWidth(2)
pdf.SetLineType("dashed")
pdf.Line(10, 30, 585, 30)

Draw oval

pdf.SetLineWidth(1)
pdf.Oval(100, 200, 500, 500)

Rotation text or image

pdf.SetX(100)
pdf.SetY(100)
pdf.Rotate(270.0, 100.0, 100.0)
pdf.Text("Hello...")
pdf.RotateReset() //reset

Password protection

package main

import (
	"log"

	"github.com/signintech/gopdf"
)


func main() {

	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{
		PageSize: gopdf.PageSizeA4, //595.28, 841.89 = A4
		Protection: gopdf.PDFProtectionConfig{
			UseProtection: true,
			Permissions: gopdf.PermissionsPrint | gopdf.PermissionsCopy | gopdf.PermissionsModify,
			OwnerPass:   []byte("123456"),
			UserPass:    []byte("123456789")},
	})

	pdf.AddPage()
	pdf.AddTTFFont("loma", "../ttf/loma.ttf")
	pdf.Cell(nil,"Hi")
	pdf.WritePdf("protect.pdf")
}

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