func addSpaces(pdf *gofpdf.Fpdf, spaces ...Space) { for _, space := range spaces { pdf.SetFillColor(space.Color.R, space.Color.G, space.Color.B) pdf.Rect(space.X+0.75, space.Y+0.5, 1, 1, "FD") } }
func addHeader(pdf *gofpdf.Fpdf, text string, size int) { if pdf == nil { return } pdf.SetFillColor(255, 255, 255) pdf.SetTextColor(0, 0, 0) pdf.SetFont("Arial", "", (float64)(size)) pdf.CellFormat((float64)(len(text)*5), (float64)(size/2), text, "0", 0, "L", true, 0, "") pdf.Ln(-1) }
func docWriter(pdf *gofpdf.Fpdf, idx int) *pdfWriter { pw := new(pdfWriter) pw.pdf = pdf pw.idx = idx if pdf.Ok() { var err error fileStr := fmt.Sprintf("%s/pdf/tutorial%02d.pdf", cnGofpdfDir, idx) pw.fl, err = os.Create(fileStr) if err != nil { pdf.SetErrorf("Error opening output file %s", fileStr) } } return pw }
func tutorialSummary(f *gofpdf.Fpdf, fileStr string) { if f.Ok() { fl, err := os.Create(fileStr) defer fl.Close() if err == nil { f.Output(fl) } else { f.SetError(err) } } if f.Ok() { fmt.Printf("Successfully generated %s\n", fileStr) } else { errPrintf("%s\n", f.Error()) } }
func createTable(pdf *gofpdf.Fpdf, header []string, data [][]string, columnSize []float64) { if pdf == nil { return } fmt.Println(header) fmt.Println("---") fmt.Println(data) fmt.Println("---") fmt.Println(columnSize) fmt.Println("---") cLen := len(columnSize) if cLen == 0 { return } fmt.Println("cLen > 0") if len(header) != 0 && cLen != len(header) { return } fmt.Println("len header 0 or eq to cLen") for _, v := range data { if len(v) != cLen { return } } fmt.Println("Check done") pdf.SetFillColor(128, 128, 128) pdf.SetTextColor(255, 255, 255) pdf.SetDrawColor(128, 0, 0) pdf.SetLineWidth(.3) pdf.SetFont("Arial", "", 12) // display header fields for i, str := range header { pdf.CellFormat(columnSize[i], 7, str, "1", 0, "C", true, 0, "") } // only advance a line if we wrote a header if len(header) > 0 { pdf.Ln(-1) } // display data /w alternating background pdf.SetFillColor(224, 235, 255) pdf.SetTextColor(0, 0, 0) pdf.SetFont("", "", 0) // Data fill := false for _, c := range data { for i, str := range c { pdf.CellFormat(columnSize[i], 6, str, "LR", 0, "", fill, 0, "") } pdf.Ln(-1) fill = !fill } }
// SaveToPdfFile creates and saves a pdf document to a file func SaveToPdfFile(filePath string, pdf *gofpdf.Fpdf) error { return pdf.OutputFileAndClose(filePath) }
func smiley(pdf *gofpdf.Fpdf, x, y float64) { pdf.Circle(0.75+x+0.5, 1+y, 0.4, "D") pdf.Circle(1.1+x, 0.8+y, 0.05, "DF") pdf.Circle(1.4+x, 0.8+y, 0.05, "DF") pdf.Arc(0.75+x+0.5, 1.1+y, 0.2, 0.2, 180, 90, 270, "FD") }
func poly(pdf *gofpdf.Fpdf, style string, points ...gofpdf.PointType) { pdf.Polygon(points, style) }
func docWriter(pdf *gofpdf.Fpdf, idx int) *pdfWriter { pw := new(pdfWriter) pw.pdf = pdf pw.idx = idx if pdf.Ok() { var err error fileStr := fmt.Sprintf("%s\\test_pdf_%02d.pdf", cnGofpdfDir, idx) pw.fl, err = os.Create(fileStr) if err != nil { pdf.SetErrorf("Error opening output file %s", fileStr) } } fileLock1 := Locker{false, "lock#1"} printIsLocked(fileLock1) fileLock1.locked = true printIsLocked(fileLock1) fileLock2 := Locker{true, "lock#2"} printIsLocked(fileLock2) return pw }