Skip to content

Instantly share code, notes, and snippets.

@innermond
Created June 23, 2019 07:04
Show Gist options
  • Save innermond/ed34fa2fba258237849bc6d63bb998dc to your computer and use it in GitHub Desktop.
Save innermond/ed34fa2fba258237849bc6d63bb998dc to your computer and use it in GitHub Desktop.
gopdf set custom page and use utf8
var err error
// set default page sra3
sra3 := gofpdf.SizeType{450, 320}
pdf := gofpdf.NewCustom(&gofpdf.InitType{UnitStr: "mm", Size: sra3})
pdf.SetCompression(false)
// Margins of page
pdf.SetMargins(0.0, 0.0, 0.0)
pdf.SetAutoPageBreak(false, 0.0)
// Cell text internal margin
// pdf.SetCellMargin(0.0)
// Font file is generated with makefont
// pdf.AddFont("BebasNeue", "", "BebasNeue.json")
// pdf.SetFont("BebasNeue", "", 10)
// UTF8 uses a .map file to get it right
/*tr := pdf.UnicodeTranslatorFromDescriptor("iso-8859-16")
// txt := "test"
txt := "ăâîșț"
widthTxtUntranslated := pdf.GetStringWidth(txt)
txt = tr(txt)
widthTxt := pdf.GetStringWidth(txt)
txt = fmt.Sprintf("%s %f %f %f", txt, widthTxtUntranslated, widthTxt, 210*k)
// pdf.Text(0.0, 5.0, txt)
// pdf.MultiCell(210*k, 30, txt, "1", "J", false)
_, h := pdf.GetFontSize()
pdf.SetLineWidth(0.5)
pdf.CellFormat(0, h, txt, "B", 1, "", false, 0, "")
pdf.Ln(h / 2)
_, h = pdf.GetFontSize()
lines := []string{}
i := 1
for i > 0 {
pdf.CellFormat(0, h, txt, "B", 1, "", false, 0, "")
pdf.Ln(h / 2)
i--
lines = append(lines, txt)
}
if len(lines) > 0 {
txt := strings.Join(lines, "\n")
pdf.SetY(30 + 15)
pdf.MultiCell(0, 1.5*15, txt, "B", "", false)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment