Blame view
pdfhelper/pdf.go
4.47 KB
1b51eed04 pdf helper |
1 2 3 4 5 6 7 8 9 10 11 12 |
package pdfhelper import ( "fmt" "strings" "git.to-net.rs/marko.tikvic/gofpdf" ) // Block ... const ( CENTER = "C" |
07daaf4e5 separate nullable... |
13 |
MIDDLE = "M" |
1b51eed04 pdf helper |
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
LEFT = "L" RIGHT = "R" TOP = "T" BOTTOM = "B" FULL = "1" NOBORDER = "" ) const ( CONTINUE = 0 NEWLINE = 1 BELLOW = 2 ) const ( cyrillicEncoding = "cp1251" latinEncoding = "cp1250" ) |
9ede5c7a9 removed unused stuff |
32 33 34 35 36 37 38 39 40 41 |
const threeDots = "\u2056\u2056\u2056" type TableCell struct { W, H float64 Text string Font, FontStyle string FontSize float64 Border string Alignment string } |
1b51eed04 pdf helper |
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
// Helper ... type Helper struct { *gofpdf.Fpdf translators map[string]func(string) string } // New ... func New(ori, unit, size string) *Helper { helper := &Helper{ Fpdf: gofpdf.New(ori, unit, size, ""), } return helper } func (pdf *Helper) LoadTranslators() { pdf.translators = make(map[string]func(string) string) pdf.translators[latinEncoding] = pdf.UnicodeTranslatorFromDescriptor(latinEncoding) pdf.translators[cyrillicEncoding] = pdf.UnicodeTranslatorFromDescriptor(cyrillicEncoding) } |
9ede5c7a9 removed unused stuff |
62 |
func (pdf *Helper) DrawCell(x, y float64, c TableCell) { |
ec4067ed8 accessories |
63 64 |
pdf.SetXY(x, y) pdf.SetFont(c.Font, c.FontStyle, c.FontSize) |
9ede5c7a9 removed unused stuff |
65 |
pdf.CellFormat(c.W, c.H, pdf.toUTF8(c.Text), c.Border, BELLOW, c.Alignment, false, 0, "") |
ec4067ed8 accessories |
66 |
} |
9ede5c7a9 removed unused stuff |
67 |
func (pdf *Helper) DrawColumn(x, y float64, cells []TableCell) { |
1b51eed04 pdf helper |
68 69 70 |
pdf.SetXY(x, y) for _, c := range cells { pdf.SetFont(c.Font, c.FontStyle, c.FontSize) |
9ede5c7a9 removed unused stuff |
71 |
pdf.CellFormat(c.W, c.H, pdf.toUTF8(c.Text), c.Border, BELLOW, c.Alignment, false, 0, "") |
1b51eed04 pdf helper |
72 73 |
} } |
9ede5c7a9 removed unused stuff |
74 |
func (pdf *Helper) DrawRow(x, y float64, cells []TableCell) { |
1b51eed04 pdf helper |
75 76 77 |
pdf.SetXY(x, y) for _, c := range cells { pdf.SetFont(c.Font, c.FontStyle, c.FontSize) |
9ede5c7a9 removed unused stuff |
78 |
pdf.CellFormat(c.W, c.H, pdf.toUTF8(c.Text), c.Border, CONTINUE, c.Alignment, false, 0, "") |
1b51eed04 pdf helper |
79 80 |
} } |
9ede5c7a9 removed unused stuff |
81 82 83 |
func (pdf *Helper) TextLength(txt, family, style string, size float64) float64 { family, _, _, _ = pdf.setCorrectFontFamily(textEncoding(txt)) return pdf.Fpdf.TextLength(txt, family, style, size) |
1b51eed04 pdf helper |
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
} func (pdf *Helper) LimitText(text, limiter string, maxWidth float64) string { parts := pdf.Fpdf.SplitText(text, maxWidth) if len(parts) > 1 { return parts[0] + limiter } return text } // InsertImage ... func (pdf *Helper) InsertImage(img string, x, y, w, h float64) { imgType := "" if parts := strings.Split(img, "."); len(parts) >= 2 { imgType = parts[len(parts)-1] } opt := gofpdf.ImageOptions{ ImageType: imgType, ReadDpi: false, AllowNegativePosition: false, } autoBreak := false // if it's not false then you can't draw the image at an arbitrary height (y position) pdf.ImageOptions(img, x, y, w, h, autoBreak, opt, 0, "") } |
9ede5c7a9 removed unused stuff |
109 110 111 112 |
func (pdf *Helper) PageHasSpace(requiredHeight float64) bool { _, h := pdf.GetPageSize() _, _, _, bot := pdf.GetMargins() return (h - bot - pdf.GetY()) > requiredHeight |
1b51eed04 pdf helper |
113 |
} |
9ede5c7a9 removed unused stuff |
114 115 116 117 118 119 |
// DrawBox ... func (pdf Helper) DrawBox(x0, y0, w, h float64) { pdf.Line(x0, y0, x0+w, y0) pdf.Line(x0+w, y0, x0+w, y0+h) pdf.Line(x0+w, y0+h, x0, y0+h) pdf.Line(x0, y0+h, x0, y0) |
1b51eed04 pdf helper |
120 |
} |
9ede5c7a9 removed unused stuff |
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
// Strana %d/{TotalPages} func (pdf *Helper) InsertPageNumber(x, y float64, format string) { num := fmt.Sprintf(format, pdf.PageNo()) pdf.DrawColumn(x, y, []TableCell{{10, 1, num, "DejaVuSans", "", 8, NOBORDER, LEFT}}) } func (pdf *Helper) SuperscriptText(x, y, cw, ch float64, text, script string) { family, style, size, sizeU := pdf.setCorrectFontFamily(textEncoding(text)) pdf.DrawCell(x, y, TableCell{cw, ch, text, family, style, size, NOBORDER, LEFT}) sx := x + pdf.TextLength(text, family, style, size) sy := y - sizeU*0.2 pdf.DrawCell(sx, sy, TableCell{cw, ch, script, family, style, size - 2, NOBORDER, LEFT}) } // toUTF8 ... func (pdf *Helper) toUTF8(s string) string { encoding := textEncoding(s) pdf.setCorrectFontFamily(encoding) translator, ok := pdf.translators[encoding] if !ok { return "" } return translator(s) |
1b51eed04 pdf helper |
146 |
} |
ec4067ed8 accessories |
147 |
func textEncoding(s string) string { |
1b51eed04 pdf helper |
148 149 150 151 152 153 154 155 |
encoding := latinEncoding runes := []rune(s) for _, r := range runes { if uint64(r) >= 0x0402 && uint64(r) <= 0x044f { encoding = cyrillicEncoding break } } |
ec4067ed8 accessories |
156 157 |
return encoding } |
1b51eed04 pdf helper |
158 159 160 |
func (pdf *Helper) setCorrectFontFamily(enc string) (family, style string, ptSize, unitSize float64) { family, style, ptSize, unitSize = pdf.GetFontInfo() if enc == cyrillicEncoding { |
ec4067ed8 accessories |
161 162 |
if !strings.HasSuffix(family, "cyrillic") { family += "cyrillic" |
1b51eed04 pdf helper |
163 164 |
} } else { |
ec4067ed8 accessories |
165 166 |
if strings.HasSuffix(family, "cyrillic") { family = strings.TrimSuffix(family, "cyrillic") |
1b51eed04 pdf helper |
167 168 169 170 171 |
} } pdf.SetFont(family, style, ptSize) return family, style, ptSize, unitSize } |