Skip to content

Instantly share code, notes, and snippets.

@BlackMocca
Last active September 13, 2019 17:17
Show Gist options
  • Select an option

  • Save BlackMocca/21392471f742d55c45dd522cc1569233 to your computer and use it in GitHub Desktop.

Select an option

Save BlackMocca/21392471f742d55c45dd522cc1569233 to your computer and use it in GitHub Desktop.
[GO Jouney EP 1] Init Pdf
package main
import (
"log"
_settingPdf "github.com/BlackMocca/go-pdf-example/pdf"
"github.com/jung-kurt/gofpdf"
)
func initPdf() (*gofpdf.Fpdf, error) {
return _settingPdf.NewPdf(gofpdf.OrientationPortrait, gofpdf.UnitCentimeter, gofpdf.PageSizeA4, _settingPdf.FontDir)
}
func main() {
var filename string
var filePath string
pdf, err := initPdf()
if err != nil {
log.Fatal("init pdf fail: ", err.Error())
}
filename = "example_1.pdf"
filePath = _settingPdf.FileOutputDesc + filename
if err = pdf.OutputFileAndClose(filePath); err != nil {
log.Fatal("Write Pdf fail :", err.Error())
}
}
package pdf
import "github.com/jung-kurt/gofpdf"
const (
FontDir = "fonts/thai-sarabun-psk/"
FileOutputDesc = "example/"
FontName = "THSarabunPSK"
FontRegularFile = "THSarabun.ttf"
FontBoldFile = "THSarabun-Bold.ttf"
FontItalicFile = "THSarabun-Italic.ttf"
FontBoldAndItalicFile = "THSarabun-Bold-Italic.ttf"
DefaultFontSize = 18
)
func NewPdf(orientation, unitStr, size, fontDir string) (*gofpdf.Fpdf, error) {
pdf := gofpdf.NewCustom(&gofpdf.InitType{
OrientationStr: orientation,
UnitStr: unitStr,
SizeStr: size,
FontDirStr: fontDir,
})
/* Font Regular */
pdf.AddUTF8Font(FontName, "", FontRegularFile)
pdf.SetFont(FontName, "", DefaultFontSize)
/* Font underline */
pdf.AddUTF8Font(FontName, "U", FontRegularFile)
/* Font Bold */
pdf.AddUTF8Font(FontName, "B", FontBoldFile)
/* Font Bold And underline */
pdf.AddUTF8Font(FontName, "BU", FontBoldFile)
/* Font Italic */
pdf.AddUTF8Font(FontName, "I", FontItalicFile)
/* Font Bold Italic */
pdf.AddUTF8Font(FontName, "BI", FontBoldAndItalicFile)
return pdf, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment