Do not skip error handling, that's why you don't see what is the reason of failure.
Please add err check to SetFont
, Text
, WritePdf
functions. In my case I found that error was coming from SetFont
since it was not found.
This code works well if you have open-sans.ttf
in your working dir:
package main
import (
"log"
"github.com/signintech/gopdf"
)
func main() {
pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
pdf.AddPage()
if err := pdf.AddTTFFont("open-sans", "open-sans.ttf"); err != nil {
log.Print(err.Error())
return
}
if err := pdf.SetFont("open-sans", "", 14); err != nil {
log.Print(err.Error())
return
}
pdf.SetX(100)
pdf.SetY(200)
data := "Line1 <br/>
" +
"Line2
" +
"Line3
" +
"Line4"
if err := pdf.Text(data); err != nil {
log.Print(err.Error())
return
}
if err := pdf.WritePdf("hello.pdf"); err != nil {
log.Print(err.Error())
return
}
}