A simple library for generating PDF written in Go lang
A simple library for generating PDF written in Go lang
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.
go get -u github.com/signintech/gopdf
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")
}
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})
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")
pdf.SetTextColor(156, 197, 140)
pdf.Cell(nil, "您好")
pdf.SetTextColorCMYK(0, 6, 14, 0)
pdf.Cell(nil, "Hello")
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")
}
…
…
pdf.SetLineWidth(2)
pdf.SetLineType("dashed")
pdf.Line(10, 30, 585, 30)
pdf.SetLineWidth(1)
pdf.Oval(100, 200, 500, 500)
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")
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
}
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
}
pdf.SetXY(100, 100)
pdf.Rotate(270.0, 100.0, 100.0)
pdf.Text("Hello...")
pdf.RotateReset() //reset
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)
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 power by package gofpdi created by @phpdave11 (thank you :smile:)
…
…
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):
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")
}
…
result:
visit https://github.com/oneplus1000/gopdfsample for more samples.
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:
Sponsor here:
https://github.com/sponsors/oneplus1000
If not, that's totally fine too.
Enjoy the library and go build cool stuff.
No open issues yet, or sync has not completed.