Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

gopdf

> 编程语言
Open source

A simple library for generating PDF written in Go lang

2.9K stars0 likes0 views
WebsiteGitHub

About

A simple library for generating PDF written in Go lang

gopdf

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

A minimum version of Go 1.13 is required.

If gopdf made your day a little easier, feel free to support the project.

Features

  • Unicode subfont embedding. (Chinese, Japanese, Korean, etc.)
  • Draw line, oval, rect, curve
  • Draw image ( jpg, png )
    • Set image mask
  • Password protection
  • Font kerning
  • Text alignment (left, center, right, justify)

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")

}

Justified text

Use Justify in CellOption.Align to stretch text to the full cell width. It works for a single line (CellWithOption) and for wrapped paragraphs (MultiCellWithOption). In a paragraph, every line is justified except the last line, which stays left-aligned. A line with no spaces, or one that already fills (or overflows) the cell, is left-aligned unchanged.

// Single justified line
pdf.CellWithOption(&gopdf.Rect{W: 400, H: 20}, text,
    gopdf.CellOption{Align: gopdf.Justify | gopdf.Top})

// Justified paragraph (last line left-aligned automatically)
pdf.MultiCellWithOption(&gopdf.Rect{W: 400, H: 200}, paragraph,
    gopdf.CellOption{Align: gopdf.Justify})

Superscript and subscript

Set gopdf.Superscript or gopdf.Subscript with SetFontWithStyle and the glyph size and baseline shift are derived from the font's own metrics (with sensible fallbacks). The logical font size keeps governing line layout, so script runs share the baseline of the surrounding text.

pdf.SetFont("font", "", 14)
pdf.Cell(nil, "E = mc")
pdf.SetFontWithStyle("font", gopdf.Superscript, 14)
pdf.Cell(nil, "2")
pdf.SetFontWithStyle("font", gopdf.Regular, 14)
pdf.Cell(nil, ", said Einstein")

Set text color using RGB color model

pdf.SetTextColor(156, 197, 140)
pdf.Cell(nil, "您好")

Set text color using CMYK color model

pdf.SetTextColorCMYK(0, 6, 14, 0)
pdf.Cell(nil, "Hello")

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.SetXY(250, 200) //move current location
	pdf.Cell(nil, "gopher and gopher") //print text

	pdf.WritePdf("image.pdf")
}

Links

…

Header and Footer

…

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)

Draw polygon

pdf.SetStrokeColor(255, 0, 0)
pdf.SetLineWidth(2)
pdf.SetFillColor(0, 255, 0)
pdf.Polygon([]gopdf.Point{{X: 10, Y: 30}, {X: 585, Y: 200}, {X: 585, Y: 250}}, "DF")

Draw rectangle with round corner

pdf.SetStrokeColor(255, 0, 0)
pdf.SetLineWidth(2)
pdf.SetFillColor(0, 255, 0)
err := pdf.Rectangle(196.6, 336.8, 398.3, 379.3, "DF", 3, 10)
if err != nil {
	return err
}

Draw rectangle with round corner using CMYK color model

pdf.SetStrokeColorCMYK(88, 49, 0, 0)
pdf.SetLineWidth(2)
pdf.SetFillColorCMYK(0, 5, 89, 0)
err := pdf.Rectangle(196.6, 336.8, 398.3, 379.3, "DF", 3, 10)
if err != nil {
	return err
}

Rotation text or image

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

Set transparency

Read about transparency in pdf (page 320, section 11)

// alpha - value of transparency, can be between `0` and `1`
// blendMode - default value is `/Normal` - read about [blendMode and kinds of its](https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf) `(page 325, section 11.3.5)`

transparency := Transparency{
	Alpha: 0.5,
	BlendModeType: "",
}
pdf.SetTransparency(transparency Transparency)

Password protection

package main

import (
	"log"

	"github.com/signintech/gopdf"
)

func main() {

	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{
		PageSize: *gopdf.PageSizeA4,
		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")
}

Import existing PDF

Import existing PDF power by package gofpdi created by @phpdave11 (thank you :smile:)

…

Possible to set Trim-box

…

Placeholder.

this function(s) made for experimental. There may be changes in the future.

With the placeholder function(s), you can create a placeholder to define a position. To make room for text to be add later.

There are 2 related function(s):

  • PlaceHolderText(...) used to create a placeholder to fill in text later.
  • FillInPlaceHoldText(...) used for filling in text into the placeholder that was created with PlaceHolderText.

Use case: For example, when you want to print the "total number of pages" on every page in pdf file, but you don't know the "total number of pages" until you have created all the pages. You can use func PlaceHolderText to create the point where you want "total number of pages" to be printed. And then when you have created all the pages so you know the "total number of pages", you call FillInPlaceHoldText(...). This function will take the text (in this case, text is "total number of pages") replace at the point that been created since func PlaceHolderText.

func main(){
    	pdf := GoPdf{}
	pdf.Start(Config{PageSize: *PageSizeA4})
	pdf.AddTTFFont("font1", "font1.ttf")
	pdf.SetFont("font1", "", 14) }

	for i := 0; i < 5; i++ {
		pdf.AddPage()
        	pdf.Br(20)
        	//create PlaceHolder
		err = pdf.PlaceHolderText("totalnumber", 30)
		if err != nil {
			log.Print(err.Error())
			return
		}

	}

    	//fillin text to PlaceHolder
	err = pdf.FillInPlaceHoldText("totalnumber",fmt.Sprintf("%d", 5), Left)
	if err != nil {
		log.Print(err.Error())
		return
	}

	pdf.WritePdf("placeholder_text.pdf")
}

Table Create

…

result:

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


❤️ Support gopdf

If gopdf saved you some time, that's awesome.

If you'd like to sponsor the project, think of it as helping me justify to my wife why I'm still staring at PDF specs at midnight.

Your support mainly goes toward:

  • ☕ coffee
  • snacks
  • and the "open source is important, I promise"

Sponsor here:
https://github.com/sponsors/oneplus1000

If not, that's totally fine too.
Enjoy the library and go build cool stuff.


Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Go

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言