diff --git a/pdfhelper/pdf.go b/pdfhelper/pdf.go index a1ccacb..dd9caad 100644 --- a/pdfhelper/pdf.go +++ b/pdfhelper/pdf.go @@ -29,6 +29,17 @@ const ( latinEncoding = "cp1250" ) +const threeDots = "\u2056\u2056\u2056" + +type TableCell struct { + W, H float64 + Text string + Font, FontStyle string + FontSize float64 + Border string + Alignment string +} + // Helper ... type Helper struct { *gofpdf.Fpdf @@ -50,106 +61,31 @@ func (pdf *Helper) LoadTranslators() { pdf.translators[cyrillicEncoding] = pdf.UnicodeTranslatorFromDescriptor(cyrillicEncoding) } -// InsertTab ... -func (pdf *Helper) InsertTab(count int, w, h float64) { - for i := 0; i < count; i++ { - pdf.Cell(w, h, "") - } -} - -// CellWithBox ... -func (pdf *Helper) CellWithBox(w, h float64, text, align string) { - //pdf.drawCellMargins(w, h) - //pdf.Cell(w, h, text) - pdf.CellFormat(w, h, pdf.ToUTF8(text), FULL, CONTINUE, align, false, 0, "") -} - -func (pdf *Helper) CellWithMargins(w, h float64, text, align string, index, of int) { - len := of - 1 - border := "" - - // if top cell - if index == 0 { - border += "T" - } - - border += "LR" // always draw these - - // if bottom cell - if index == len { - border += "B" - } - - pdf.CellFormat(w, h, pdf.ToUTF8(text), border, CONTINUE, align, false, 0, "") -} - -// MultiRowCellWithBox ... -func (pdf *Helper) MultiRowCellWithBox(w, h float64, text []string, align string) { - pdf.drawCellMargins(w, h*float64(len(text))) - for i := range text { - pdf.CellFormat(w, h, pdf.ToUTF8(text[i]), "", BELLOW, align, false, 0, "") - } -} - -// CellFormat(w, h, text, borders, newLine, fill) - -type FormatedCell struct { - W, H float64 - Text string - Font, FontStyle string - FontSize float64 - Border string - Alignment string -} - -func (pdf *Helper) FCell(x, y float64, c FormatedCell) { +func (pdf *Helper) DrawCell(x, y float64, c TableCell) { pdf.SetXY(x, y) pdf.SetFont(c.Font, c.FontStyle, c.FontSize) - pdf.CellFormat(c.W, c.H, pdf.ToUTF8(c.Text), c.Border, BELLOW, c.Alignment, false, 0, "") + pdf.CellFormat(c.W, c.H, pdf.toUTF8(c.Text), c.Border, BELLOW, c.Alignment, false, 0, "") } -func (pdf *Helper) Column(x, y float64, cells []FormatedCell) { +func (pdf *Helper) DrawColumn(x, y float64, cells []TableCell) { pdf.SetXY(x, y) for _, c := range cells { pdf.SetFont(c.Font, c.FontStyle, c.FontSize) - pdf.CellFormat(c.W, c.H, pdf.ToUTF8(c.Text), c.Border, BELLOW, c.Alignment, false, 0, "") - //pdf.CellFormat(c.w, c.h, c.text, c.border, BELLOW, align, false, 0, "") + pdf.CellFormat(c.W, c.H, pdf.toUTF8(c.Text), c.Border, BELLOW, c.Alignment, false, 0, "") } } -// NewLine ... -func (pdf *Helper) NewLine(h float64) { - pdf.Ln(h) -} - -// WriteColumns ... -func (pdf *Helper) WriteColumns(align string, cols []PDFCell) { - for _, c := range cols { - pdf.CellFormat(c.width, c.height, pdf.ToUTF8(c.data), "", CONTINUE, align, false, 0, "") - } -} - -func (pdf *Helper) Row(x, y float64, cells []FormatedCell) { +func (pdf *Helper) DrawRow(x, y float64, cells []TableCell) { pdf.SetXY(x, y) for _, c := range cells { pdf.SetFont(c.Font, c.FontStyle, c.FontSize) - pdf.CellFormat(c.W, c.H, pdf.ToUTF8(c.Text), c.Border, CONTINUE, c.Alignment, false, 0, "") + pdf.CellFormat(c.W, c.H, pdf.toUTF8(c.Text), c.Border, CONTINUE, c.Alignment, false, 0, "") } } -const threeDots = "\u2056\u2056\u2056" - -// WriteColumnsWithAlignment ... -func (pdf *Helper) WriteColumnsWithAlignment(cols []PDFCellAligned) { - for _, c := range cols { - lines := pdf.SplitText(c.data, c.width) - if len(lines) == 1 { - pdf.CellFormat(c.width, c.height, pdf.ToUTF8(lines[0]), "", CONTINUE, c.alignment, false, 0, "") - } else { - pdf.CellFormat(c.width, c.height, pdf.ToUTF8(lines[0]+threeDots), "", CONTINUE, c.alignment, false, 0, "") - } - } - +func (pdf *Helper) TextLength(txt, family, style string, size float64) float64 { + family, _, _, _ = pdf.setCorrectFontFamily(textEncoding(txt)) + return pdf.Fpdf.TextLength(txt, family, style, size) } func (pdf *Helper) LimitText(text, limiter string, maxWidth float64) string { @@ -176,22 +112,45 @@ func (pdf *Helper) InsertImage(img string, x, y, w, h float64) { pdf.ImageOptions(img, x, y, w, h, autoBreak, opt, 0, "") } -// PDFCell ... -type PDFCell struct { - data string - height, width float64 +func (pdf *Helper) PageHasSpace(requiredHeight float64) bool { + _, h := pdf.GetPageSize() + _, _, _, bot := pdf.GetMargins() + return (h - bot - pdf.GetY()) > requiredHeight } -// PDFCellAligned ... -type PDFCellAligned struct { - alignment string - data string - height, width float64 +// 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) } -func (pdf *Helper) TextLength(txt, family, style string, size float64) float64 { - family, _, _, _ = pdf.setCorrectFontFamily(textEncoding(txt)) - return pdf.Fpdf.TextLength(txt, family, style, size) +// 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) } func textEncoding(s string) string { @@ -206,17 +165,6 @@ func textEncoding(s string) string { return encoding } -// 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) -} - func (pdf *Helper) setCorrectFontFamily(enc string) (family, style string, ptSize, unitSize float64) { family, style, ptSize, unitSize = pdf.GetFontInfo() if enc == cyrillicEncoding { @@ -231,46 +179,3 @@ func (pdf *Helper) setCorrectFontFamily(enc string) (family, style string, ptSiz pdf.SetFont(family, style, ptSize) return family, style, ptSize, unitSize } - -func (pdf *Helper) PageHasSpace(requiredHeight float64) bool { - _, h := pdf.GetPageSize() - _, _, _, bot := pdf.GetMargins() - return (h - bot - pdf.GetY()) > requiredHeight -} - -func (pdf *Helper) imageCenterOffset(w, h float64) (x, y float64) { - pageW, pageH := pdf.GetPageSize() - x = pageW/2.0 - w/2.0 - y = pageH/2.0 - h/2.0 - return x, y -} - -// call before drawing the cell -func (pdf *Helper) drawCellMargins(cw, ch float64) { - x0, y0 := pdf.GetX(), pdf.GetY() - pdf.DrawBox(x0, y0, cw, ch) -} - -// 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) -} - -// Strana %d/{TotalPages} -func (pdf *Helper) InsertPageNumber(x, y float64, format string) { - num := fmt.Sprintf(format, pdf.PageNo()) - pdf.Column(x, y, []FormatedCell{{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.FCell(x, y, FormatedCell{cw, ch, text, family, style, size, NOBORDER, LEFT}) - - sx := x + pdf.TextLength(text, family, style, size) - sy := y - sizeU*0.2 - pdf.FCell(sx, sy, FormatedCell{cw, ch, script, family, style, size - 2, NOBORDER, LEFT}) -}