// This example demonstrates clipping. func ExampleFpdf_ClipText() { pdf := gofpdf.New("", "", "", "") y := 10.0 pdf.AddPage() pdf.SetFont("Helvetica", "", 24) pdf.SetXY(0, y) pdf.ClipText(10, y+12, "Clipping examples", false) pdf.RadialGradient(10, y, 100, 20, 128, 128, 160, 32, 32, 48, 0.25, 0.5, 0.25, 0.5, 0.2) pdf.ClipEnd() y += 12 pdf.SetFont("Helvetica", "B", 120) pdf.SetDrawColor(64, 80, 80) pdf.SetLineWidth(.5) pdf.ClipText(10, y+40, pdf.String(), true) pdf.RadialGradient(10, y, 200, 50, 220, 220, 250, 80, 80, 220, 0.25, 0.5, 0.25, 0.5, 1) pdf.ClipEnd() y += 55 pdf.ClipRect(10, y, 105, 20, true) pdf.SetFillColor(255, 255, 255) pdf.Rect(10, y, 105, 20, "F") pdf.ClipCircle(40, y+10, 15, false) pdf.RadialGradient(25, y, 30, 30, 220, 250, 220, 40, 60, 40, 0.3, 0.85, 0.3, 0.85, 0.5) pdf.ClipEnd() pdf.ClipEllipse(80, y+10, 20, 15, false) pdf.RadialGradient(60, y, 40, 30, 250, 220, 220, 60, 40, 40, 0.3, 0.85, 0.3, 0.85, 0.5) pdf.ClipEnd() pdf.ClipEnd() y += 28 pdf.ClipEllipse(26, y+10, 16, 10, true) pdf.Image(imageFile("logo.jpg"), 10, y, 32, 0, false, "JPG", 0, "") pdf.ClipEnd() pdf.ClipCircle(60, y+10, 10, true) pdf.RadialGradient(50, y, 20, 20, 220, 220, 250, 40, 40, 60, 0.3, 0.7, 0.3, 0.7, 0.5) pdf.ClipEnd() pdf.ClipPolygon([]gofpdf.PointType{{80, y + 20}, {90, y}, {100, y + 20}}, true) pdf.LinearGradient(80, y, 20, 20, 250, 220, 250, 60, 40, 60, 0.5, 1, 0.5, 0.5) pdf.ClipEnd() y += 30 pdf.SetLineWidth(.1) pdf.SetDrawColor(180, 180, 180) pdf.ClipRoundedRect(10, y, 120, 20, 5, true) pdf.RadialGradient(10, y, 120, 20, 255, 255, 255, 240, 240, 220, 0.25, 0.75, 0.25, 0.75, 0.5) pdf.SetXY(5, y-5) pdf.SetFont("Times", "", 12) pdf.MultiCell(130, 5, lorem(), "", "", false) pdf.ClipEnd() fileStr := exampleFilename("clip") err := pdf.OutputFileAndClose(fileStr) summary(err, fileStr) // Output: // Successfully generated pdf/clip.pdf }
func createPdf() (pdf *gofpdf.Fpdf) { pdf = gofpdf.New("L", "mm", "A4", "") pdf.SetFont("Helvetica", "", 12) pdf.SetFillColor(200, 200, 220) pdf.AddPage() return }
func main() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.SetHeaderFunc(func() { // pdf.Image(example.ImageFile("logo.png"), 10, 6, 30, 0, false, "", 0, "") pdf.Image("logo.png", 10, 6, 30, 0, false, "", 0, "") pdf.SetY(5) pdf.SetFont("Arial", "B", 15) pdf.Cell(80, 0, "") pdf.CellFormat(30, 10, "Title", "1", 0, "C", false, 0, "") pdf.Ln(20) }) pdf.SetFooterFunc(func() { pdf.SetY(-15) pdf.SetFont("Arial", "I", 8) pdf.CellFormat(0, 10, fmt.Sprintf("Page %d/{nb}", pdf.PageNo()), "", 0, "C", false, 0, "") }) pdf.AliasNbPages("") pdf.AddPage() pdf.SetFont("Times", "", 12) for j := 1; j <= 40; j++ { pdf.CellFormat(0, 10, fmt.Sprintf("Printing line number %d", j), "", 1, "", false, 0, "") } fileStr := "fpdf_addpage.pdf" // example.Filename("Fpdf_AddPage") err := pdf.OutputFileAndClose(fileStr) fmt.Println(err) // example.Summary(err, fileStr) }
func init() { p := gofpdf.New("P", "mm", "A4", "") pngtp = p.ImageTypeFromMime("image/png") jpgtp = p.ImageTypeFromMime("image/jpeg") formBytes = rice.MustFindBox("forms").MustBytes("definitief.jpg") }
// This example demonstrates Stefan Schroeder's code to control vertical // alignment. func ExampleFpdf_tutorial21() { type recType struct { align, txt string } recList := []recType{ recType{"TL", "top left"}, recType{"TC", "top center"}, recType{"TR", "top right"}, recType{"LM", "middle left"}, recType{"CM", "middle center"}, recType{"RM", "middle right"}, recType{"BL", "bottom left"}, recType{"BC", "bottom center"}, recType{"BR", "bottom right"}, } pdf := gofpdf.New("P", "mm", "A4", "") // A4 210.0 x 297.0 pdf.SetFont("Helvetica", "", 16) linkStr := "" for pageJ := 0; pageJ < 2; pageJ++ { pdf.AddPage() pdf.SetMargins(10, 10, 10) pdf.SetAutoPageBreak(false, 0) borderStr := "1" for _, rec := range recList { pdf.SetXY(20, 20) pdf.CellFormat(170, 257, rec.txt, borderStr, 0, rec.align, false, 0, linkStr) borderStr = "" } linkStr = "https://github.com/jung-kurt/gofpdf" } pdf.OutputAndClose(docWriter(pdf, 21)) // Output: // Successfully generated pdf/tutorial21.pdf }
// This example demonstrates document layers. The initial visibility of a layer // is specified with the second parameter to AddLayer(). The layer list // displayed by the document reader allows layer visibility to be controlled // interactively. func ExampleFpdf_tutorial26() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.AddPage() pdf.SetFont("Arial", "", 15) pdf.Write(8, "This line doesn't belong to any layer.\n") // Define layers l1 := pdf.AddLayer("Layer 1", true) l2 := pdf.AddLayer("Layer 2", true) // Open layer pane in PDF viewer pdf.OpenLayerPane() // First layer pdf.BeginLayer(l1) pdf.Write(8, "This line belongs to layer 1.\n") pdf.EndLayer() // Second layer pdf.BeginLayer(l2) pdf.Write(8, "This line belongs to layer 2.\n") pdf.EndLayer() // First layer again pdf.BeginLayer(l1) pdf.Write(8, "This line belongs to layer 1 again.\n") pdf.EndLayer() pdf.OutputAndClose(docWriter(pdf, 26)) // Output: // Successfully generated pdf/tutorial26.pdf }
// This example demonstrates the conversion of UTF-8 strings to an 8-bit font // encoding. func ExampleFpdf_tutorial23() { pdf := gofpdf.New("P", "mm", "A4", cnFontDir) // A4 210.0 x 297.0 // See documentation for details on how to generate fonts pdf.AddFont("Helvetica-1251", "", "helvetica_1251.json") pdf.AddFont("Helvetica-1253", "", "helvetica_1253.json") fontSize := 16.0 pdf.SetFont("Helvetica", "", fontSize) ht := pdf.PointConvert(fontSize) tr := pdf.UnicodeTranslatorFromDescriptor("") // "" defaults to "cp1252" write := func(str string) { pdf.CellFormat(190, ht, tr(str), "", 1, "C", false, 0, "") pdf.Ln(ht) } pdf.AddPage() str := `Gofpdf provides a translator that will convert any UTF-8 code point ` + `that is present in the specified code page.` pdf.MultiCell(190, ht, str, "", "L", false) pdf.Ln(2 * ht) write("Voix ambiguë d'un cœur qui au zéphyr préfère les jattes de kiwi.") write("Falsches Üben von Xylophonmusik quält jeden größeren Zwerg.") write("Heizölrückstoßabdämpfung") write("Forårsjævndøgn / Efterårsjævndøgn") pdf.SetFont("Helvetica-1251", "", fontSize) // Name matches one specified in AddFont() tr = pdf.UnicodeTranslatorFromDescriptor("cp1251") write("Съешь же ещё этих мягких французских булок, да выпей чаю.") pdf.SetFont("Helvetica-1253", "", fontSize) tr = pdf.UnicodeTranslatorFromDescriptor("cp1253") write("Θέλει αρετή και τόλμη η ελευθερία. (Ανδρέας Κάλβος)") pdf.OutputAndClose(docWriter(pdf, 23)) // Output: // Successfully generated pdf/tutorial23.pdf }
// Header, footer and page-breaking func ExampleFpdf_tutorial02() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.SetHeaderFunc(func() { pdf.Image(imageFile("logo.png"), 10, 6, 30, 0, false, "", 0, "") pdf.SetY(5) pdf.SetFont("Arial", "B", 15) pdf.Cell(80, 0, "") pdf.CellFormat(30, 10, "Title", "1", 0, "C", false, 0, "") pdf.Ln(20) }) pdf.SetFooterFunc(func() { pdf.SetY(-15) pdf.SetFont("Arial", "I", 8) pdf.CellFormat(0, 10, fmt.Sprintf("Page %d/{nb}", pdf.PageNo()), "", 0, "C", false, 0, "") }) pdf.AliasNbPages("") pdf.AddPage() pdf.SetFont("Times", "", 12) for j := 1; j <= 40; j++ { pdf.CellFormat(0, 10, fmt.Sprintf("Printing line number %d", j), "", 1, "", false, 0, "") } pdf.OutputAndClose(docWriter(pdf, 2)) // Output: // Successfully generated pdf/tutorial02.pdf }
// This example demonsrates the generation of headers, footers and page breaks. func ExampleFpdf_AddPage() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.SetHeaderFunc(func() { pdf.Image(imageFile("logo.png"), 10, 6, 30, 0, false, "", 0, "") pdf.SetY(5) pdf.SetFont("Arial", "B", 15) pdf.Cell(80, 0, "") pdf.CellFormat(30, 10, "Title", "1", 0, "C", false, 0, "") pdf.Ln(20) }) pdf.SetFooterFunc(func() { pdf.SetY(-15) pdf.SetFont("Arial", "I", 8) pdf.CellFormat(0, 10, fmt.Sprintf("Page %d/{nb}", pdf.PageNo()), "", 0, "C", false, 0, "") }) pdf.AliasNbPages("") pdf.AddPage() pdf.SetFont("Times", "", 12) for j := 1; j <= 40; j++ { pdf.CellFormat(0, 10, fmt.Sprintf("Printing line number %d", j), "", 1, "", false, 0, "") } fileStr := exampleFilename("Fpdf_AddPage") err := pdf.OutputFileAndClose(fileStr) summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_AddPage.pdf }
// This example demonstrates internal and external links with and without basic // HTML. func ExampleFpdf_tutorial06() { pdf := gofpdf.New("P", "mm", "A4", "") // First page: manual local link pdf.AddPage() pdf.SetFont("Helvetica", "", 20) _, lineHt := pdf.GetFontSize() pdf.Write(lineHt, "To find out what's new in this tutorial, click ") pdf.SetFont("", "U", 0) link := pdf.AddLink() pdf.WriteLinkID(lineHt, "here", link) pdf.SetFont("", "", 0) // Second page: image link and basic HTML with link pdf.AddPage() pdf.SetLink(link, 0, -1) pdf.Image(imageFile("logo.png"), 10, 12, 30, 0, false, "", 0, "http://www.fpdf.org") pdf.SetLeftMargin(45) pdf.SetFontSize(14) _, lineHt = pdf.GetFontSize() htmlStr := `You can now easily print text mixing different styles: <b>bold</b>, ` + `<i>italic</i>, <u>underlined</u>, or <b><i><u>all at once</u></i></b>!<br><br>` + `You can also insert links on text, such as ` + `<a href="http://www.fpdf.org">www.fpdf.org</a>, or on an image: click on the logo.` html := pdf.HTMLBasicNew() html.Write(lineHt, htmlStr) pdf.OutputAndClose(docWriter(pdf, 6)) // Output: // Successfully generated pdf/tutorial06.pdf }
// Generates a PDF-formatted report of the defenses schedule. func DefensesPdfReportHandler(w http.ResponseWriter, r *http.Request) { if !UserIsReader(w, r) { return } vars := mux.Vars(r) matches, err := db.GetMatchesByType(vars["type"]) if err != nil { handleWebErr(w, err) return } // The widths of the table columns in mm, stored here so that they can be referenced for each row. colWidths := map[string]float64{"Type": 25, "Match": 15, "Defense": 15.5} rowHeight := 6.5 pdf := gofpdf.New("P", "mm", "Letter", "font") pdf.AddPage() // Render table header row. pdf.SetFont("Arial", "B", 10) pdf.SetFillColor(220, 220, 220) pdf.CellFormat(195, rowHeight, "Defenses Schedule - "+eventSettings.Name, "", 1, "C", false, 0, "") pdf.CellFormat(colWidths["Type"], rowHeight, "Type", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Match"], rowHeight, "Match", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, "Red 1", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, "Red 2", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, "Red 3", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, "Red 4", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, "Red 5", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, "Blue 1", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, "Blue 2", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, "Blue 3", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, "Blue 4", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, "Blue 5", "1", 1, "C", true, 0, "") pdf.SetFont("Arial", "", 10) for _, match := range matches { // Render match defenses row. pdf.CellFormat(colWidths["Type"], rowHeight, match.CapitalizedType(), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Match"], rowHeight, match.DisplayName, "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, match.RedDefense1, "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, match.RedDefense2, "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, match.RedDefense3, "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, match.RedDefense4, "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, match.RedDefense5, "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, match.BlueDefense1, "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, match.BlueDefense2, "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, match.BlueDefense3, "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, match.BlueDefense4, "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, match.BlueDefense5, "1", 1, "C", false, 0, "") } // Write out the PDF file as the HTTP response. w.Header().Set("Content-Type", "application/pdf") err = pdf.Output(w) if err != nil { handleWebErr(w, err) return } }
// This example demonstrates document layers. The initial visibility of a layer // is specified with the second parameter to AddLayer(). The layer list // displayed by the document reader allows layer visibility to be controlled // interactively. func ExampleFpdf_AddLayer() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.AddPage() pdf.SetFont("Arial", "", 15) pdf.Write(8, "This line doesn't belong to any layer.\n") // Define layers l1 := pdf.AddLayer("Layer 1", true) l2 := pdf.AddLayer("Layer 2", true) // Open layer pane in PDF viewer pdf.OpenLayerPane() // First layer pdf.BeginLayer(l1) pdf.Write(8, "This line belongs to layer 1.\n") pdf.EndLayer() // Second layer pdf.BeginLayer(l2) pdf.Write(8, "This line belongs to layer 2.\n") pdf.EndLayer() // First layer again pdf.BeginLayer(l1) pdf.Write(8, "This line belongs to layer 1 again.\n") pdf.EndLayer() fileStr := exampleFilename("Fpdf_AddLayer") err := pdf.OutputFileAndClose(fileStr) summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_AddLayer.pdf }
// SetGrid sets all page and grid related specifications required to place // content. This must be set before any Content() calls are made. func (r *Report) SetGrid( orientation int, pageSize int, unit int, margin float64, columnCount int, gutterWidth float64, lineHeight float64, ) { fontPath := r.FontSourcePath r.Grid = Grid{ Orientation: orientation, PageSize: pageSize, Unit: unit, ColumnCount: columnCount, GutterWidth: gutterWidth, Margin: margin, LineHeight: lineHeight, } pdf := gofpdf.New( r.Grid.convertOrientation(), r.Grid.convertUnit(), r.Grid.convertPageSize(), fontPath, ) r.Pdf = pdf r.Pdf.SetMargins(margin, margin, margin) pageWidth, pageHeight := r.Pdf.GetPageSize() r.Grid.PageWidth = pageWidth r.Grid.PageHeight = pageHeight r.Grid.CalculateColumns() }
// This example demonstrates the use of characters in the high range of the // Windows-1252 code page (gofdpf default). See the following example (23) for // a way to do this automatically. func ExampleFpdf_tutorial22() { pdf := gofpdf.New("P", "mm", "A4", "") // A4 210.0 x 297.0 fontSize := 16.0 pdf.SetFont("Helvetica", "", fontSize) ht := pdf.PointConvert(fontSize) write := func(str string) { pdf.CellFormat(190, ht, str, "", 1, "C", false, 0, "") pdf.Ln(ht) } pdf.AddPage() htmlStr := `Until gofpdf supports UTF-8 encoded source text, source text needs ` + `to be specified with all special characters escaped to match the code page ` + `layout of the currently selected font. By default, gofdpf uses code page 1252.` + ` See <a href="http://en.wikipedia.org/wiki/Windows-1252">Wikipedia</a> for ` + `a table of this layout.` html := pdf.HTMLBasicNew() html.Write(ht, htmlStr) pdf.Ln(2 * ht) write("Voix ambigu\xeb d'un c\x9cur qui au z\xe9phyr pr\xe9f\xe8re les jattes de kiwi.") write("Falsches \xdcben von Xylophonmusik qu\xe4lt jeden gr\xf6\xdferen Zwerg.") write("Heiz\xf6lr\xfccksto\xdfabd\xe4mpfung") write("For\xe5rsj\xe6vnd\xf8gn / Efter\xe5rsj\xe6vnd\xf8gn") pdf.OutputAndClose(docWriter(pdf, 22)) // Output: // Successfully generated pdf/tutorial22.pdf }
// Example to demonstrate Bruno Michel's line splitting function. func ExampleFpdf_tutorial19() { const ( fontPtSize = 18.0 wd = 100.0 ) pdf := gofpdf.New("P", "mm", "A4", "") // A4 210.0 x 297.0 pdf.SetFont("Times", "", fontPtSize) _, lineHt := pdf.GetFontSize() pdf.AddPage() pdf.SetMargins(10, 10, 10) lines := pdf.SplitLines([]byte(lorem()), wd) ht := float64(len(lines)) * lineHt y := (297.0 - ht) / 2.0 pdf.SetDrawColor(128, 128, 128) pdf.SetFillColor(255, 255, 210) x := (210.0 - (wd + 40.0)) / 2.0 pdf.Rect(x, y-20.0, wd+40.0, ht+40.0, "FD") pdf.SetY(y) for _, line := range lines { pdf.CellFormat(190.0, lineHt, string(line), "", 1, "C", false, 0, "") } pdf.OutputAndClose(docWriter(pdf, 19)) // Output: // Successfully generated pdf/tutorial19.pdf }
func NewPDF(name string) *pdf { p := &pdf{ name: name, fpdf: gofpdf.New("P", "pt", "A4", ""), } p.fpdf.RegisterImageReader(formName, jpgtp, bytes.NewBuffer(formBytes)) return p }
// This examples demonstrates Landscape mode with images. func ExampleFpdf_SetAcceptPageBreakFunc() { var y0 float64 var crrntCol int loremStr := lorem() pdf := gofpdf.New("L", "mm", "A4", "") const ( pageWd = 297.0 // A4 210.0 x 297.0 margin = 10.0 gutter = 4 colNum = 3 colWd = (pageWd - 2*margin - (colNum-1)*gutter) / colNum ) setCol := func(col int) { crrntCol = col x := margin + float64(col)*(colWd+gutter) pdf.SetLeftMargin(x) pdf.SetX(x) } pdf.SetHeaderFunc(func() { titleStr := "gofpdf" pdf.SetFont("Helvetica", "B", 48) wd := pdf.GetStringWidth(titleStr) + 6 pdf.SetX((pageWd - wd) / 2) pdf.SetTextColor(128, 128, 160) pdf.Write(12, titleStr[:2]) pdf.SetTextColor(128, 128, 128) pdf.Write(12, titleStr[2:]) pdf.Ln(20) y0 = pdf.GetY() }) pdf.SetAcceptPageBreakFunc(func() bool { if crrntCol < colNum-1 { setCol(crrntCol + 1) pdf.SetY(y0) // Start new column, not new page return false } setCol(0) return true }) pdf.AddPage() pdf.SetFont("Times", "", 12) for j := 0; j < 20; j++ { if j == 1 { pdf.Image(imageFile("fpdf.png"), -1, 0, colWd, 0, true, "", 0, "") } else if j == 5 { pdf.Image(imageFile("golang-gopher.png"), -1, 0, colWd, 0, true, "", 0, "") } pdf.MultiCell(colWd, 5, loremStr, "", "", false) pdf.Ln(-1) } fileStr := exampleFilename("Fpdf_SetAcceptPageBreakFunc_landscape") err := pdf.OutputFileAndClose(fileStr) summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_SetAcceptPageBreakFunc_landscape.pdf }
// Generates a PDF-formatted report of the qualification rankings. func RankingsPdfReportHandler(w http.ResponseWriter, r *http.Request) { if !UserIsReader(w, r) { return } rankings, err := db.GetAllRankings() if err != nil { handleWebErr(w, err) return } // The widths of the table columns in mm, stored here so that they can be referenced for each row. colWidths := map[string]float64{"Rank": 13, "Team": 23, "RP": 20, "Auto": 20, "SC": 21, "Goal": 20, "Defense": 20, "W-L-T": 20, "DQ": 20, "Played": 20} rowHeight := 6.5 pdf := gofpdf.New("P", "mm", "Letter", "font") pdf.AddPage() // Render table header row. pdf.SetFont("Arial", "B", 10) pdf.SetFillColor(220, 220, 220) pdf.CellFormat(195, rowHeight, "Team Standings - "+eventSettings.Name, "", 1, "C", false, 0, "") pdf.CellFormat(colWidths["Rank"], rowHeight, "Rank", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Team"], rowHeight, "Team", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["RP"], rowHeight, "RB", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Auto"], rowHeight, "Auto", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["SC"], rowHeight, "Scale/Chal.", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Goal"], rowHeight, "Goal", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, "Defense", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["W-L-T"], rowHeight, "W-L-T", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["DQ"], rowHeight, "DQ", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Played"], rowHeight, "Played", "1", 1, "C", true, 0, "") for _, ranking := range rankings { // Render ranking info row. pdf.SetFont("Arial", "B", 10) pdf.CellFormat(colWidths["Rank"], rowHeight, strconv.Itoa(ranking.Rank), "1", 0, "C", false, 0, "") pdf.SetFont("Arial", "", 10) pdf.CellFormat(colWidths["Team"], rowHeight, strconv.Itoa(ranking.TeamId), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["RP"], rowHeight, strconv.Itoa(ranking.RankingPoints), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Auto"], rowHeight, strconv.Itoa(ranking.AutoPoints), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["SC"], rowHeight, strconv.Itoa(ranking.ScaleChallengePoints), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Goal"], rowHeight, strconv.Itoa(ranking.GoalPoints), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Defense"], rowHeight, strconv.Itoa(ranking.DefensePoints), "1", 0, "C", false, 0, "") record := fmt.Sprintf("%d-%d-%d", ranking.Wins, ranking.Losses, ranking.Ties) pdf.CellFormat(colWidths["W-L-T"], rowHeight, record, "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["DQ"], rowHeight, strconv.Itoa(ranking.Disqualifications), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Played"], rowHeight, strconv.Itoa(ranking.Played), "1", 1, "C", false, 0, "") } // Write out the PDF file as the HTTP response. w.Header().Set("Content-Type", "application/pdf") err = pdf.Output(w) if err != nil { handleWebErr(w, err) return } }
// Generates a PDF-formatted report of the qualification rankings. func RankingsPdfReportHandler(w http.ResponseWriter, r *http.Request) { if !UserIsReader(w, r) { return } rankings, err := db.GetAllRankings() if err != nil { handleWebErr(w, err) return } // The widths of the table columns in mm, stored here so that they can be referenced for each row. colWidths := map[string]float64{"Rank": 13, "Team": 23, "QA": 20, "Coop": 20, "Auto": 20, "Container": 20, "Tote": 20, "Litter": 20, "DQ": 20, "Played": 20} rowHeight := 6.5 pdf := gofpdf.New("P", "mm", "Letter", "font") pdf.AddPage() // Render table header row. pdf.SetFont("Arial", "B", 10) pdf.SetFillColor(220, 220, 220) pdf.CellFormat(195, rowHeight, "Team Standings - "+eventSettings.Name, "", 1, "C", false, 0, "") pdf.CellFormat(colWidths["Rank"], rowHeight, "Rank", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Team"], rowHeight, "Team", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["QA"], rowHeight, "QA", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Coop"], rowHeight, "Coop", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Auto"], rowHeight, "Auto", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Container"], rowHeight, "Container", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Tote"], rowHeight, "Tote", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Litter"], rowHeight, "Litter", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["DQ"], rowHeight, "DQ", "1", 0, "C", true, 0, "") pdf.CellFormat(colWidths["Played"], rowHeight, "Played", "1", 1, "C", true, 0, "") for _, ranking := range rankings { // Render ranking info row. pdf.SetFont("Arial", "B", 10) pdf.CellFormat(colWidths["Rank"], rowHeight, strconv.Itoa(ranking.Rank), "1", 0, "C", false, 0, "") pdf.SetFont("Arial", "", 10) pdf.CellFormat(colWidths["Team"], rowHeight, strconv.Itoa(ranking.TeamId), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["QA"], rowHeight, strconv.FormatFloat(ranking.QualificationAverage, 'f', 2, 64), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Coop"], rowHeight, strconv.Itoa(ranking.CoopertitionPoints), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Auto"], rowHeight, strconv.Itoa(ranking.AutoPoints), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Container"], rowHeight, strconv.Itoa(ranking.ContainerPoints), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Tote"], rowHeight, strconv.Itoa(ranking.TotePoints), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Litter"], rowHeight, strconv.Itoa(ranking.LitterPoints), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["DQ"], rowHeight, strconv.Itoa(ranking.Disqualifications), "1", 0, "C", false, 0, "") pdf.CellFormat(colWidths["Played"], rowHeight, strconv.Itoa(ranking.Played), "1", 1, "C", false, 0, "") } // Write out the PDF file as the HTTP response. w.Header().Set("Content-Type", "application/pdf") err = pdf.Output(w) if err != nil { handleWebErr(w, err) return } }
// Non-standard font func ExampleFpdf_tutorial07() { pdf := gofpdf.New("P", "mm", "A4", cnFontDir) pdf.AddFont("Calligrapher", "", "calligra.json") pdf.AddPage() pdf.SetFont("Calligrapher", "", 35) pdf.Cell(0, 10, "Enjoy new fonts with FPDF!") pdf.OutputAndClose(docWriter(pdf, 7)) // Output: // Successfully generated pdf/tutorial07.pdf }
// This example demonstrates document protection. func ExampleFpdf_tutorial24() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.SetProtection(gofpdf.CnProtectPrint, "123", "abc") pdf.AddPage() pdf.SetFont("Arial", "", 12) pdf.Write(10, "Password-protected.") pdf.OutputAndClose(docWriter(pdf, 24)) // Output: // Successfully generated pdf/tutorial24.pdf }
func writePdf() { pdf := gofpdf.New("P", "mm", "A4", "../font") pdf.AddPage() pdf.SetFont("Arial", "B", 16) pdf.Cell(40, 5, "Hello, world") pdf.Cell(80, 40, "Hello, world. line #2") pdf.Cell(0, 0, "Hello, world. line #3") // pdf.Output(os.Stdout) pdf.OutputAndClose(docWriter(pdf, 5)) }
// This example demonstrates the generation of a simple PDF document. Note that // since only core fonts are used (in this case Arial, a synonym for // Helvetica), an empty string can be specified for the font directory in the // call to New(). Note also that the exampleFilename and summary functions are // local to the test file and are not part of the gofpdf library. If an error // occurs at some point during the construction of the document, subsequent // method calls exit immediately and the error is finally retreived with the // output call where it can be handled by the application. func Example() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.AddPage() pdf.SetFont("Arial", "B", 16) pdf.Cell(40, 10, "Hello World!") fileStr := exampleFilename("basic") err := pdf.OutputFileAndClose(fileStr) summary(err, fileStr) // Output: // Successfully generated pdf/basic.pdf }
// NewPdf creates a new pdf document with the draw2d fontfolder, adds // a page and set fill color to white. func NewPdf(orientationStr, unitStr, sizeStr string) *gofpdf.Fpdf { pdf := gofpdf.New(orientationStr, unitStr, sizeStr, draw2d.GetFontFolder()) // to be compatible with draw2d pdf.SetMargins(0, 0, 0) pdf.SetDrawColor(0, 0, 0) pdf.SetFillColor(255, 255, 255) pdf.SetLineCapStyle("round") pdf.SetLineJoinStyle("round") pdf.SetLineWidth(1) pdf.AddPage() return pdf }
// This example demonstrates password protection for documents. func ExampleFpdf_SetProtection() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.SetProtection(gofpdf.CnProtectPrint, "123", "abc") pdf.AddPage() pdf.SetFont("Arial", "", 12) pdf.Write(10, "Password-protected.") fileStr := exampleFilename("Fpdf_SetProtection") err := pdf.OutputFileAndClose(fileStr) summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_SetProtection.pdf }
// This example demonstrates the use of a non-standard font. func ExampleFpdf_AddFont() { pdf := gofpdf.New("P", "mm", "A4", cnFontDir) pdf.AddFont("Calligrapher", "", "calligra.json") pdf.AddPage() pdf.SetFont("Calligrapher", "", 35) pdf.Cell(0, 10, "Enjoy new fonts with FPDF!") fileStr := exampleFilename("Fpdf_AddFont") err := pdf.OutputFileAndClose(fileStr) summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_AddFont.pdf }
// This example demonstrates Stefan Schroeder's code to control vertical // alignment. func ExampleFpdf_CellFormat_2() { type recType struct { align, txt string } recList := []recType{ recType{"TL", "top left"}, recType{"TC", "top center"}, recType{"TR", "top right"}, recType{"LM", "middle left"}, recType{"CM", "middle center"}, recType{"RM", "middle right"}, recType{"BL", "bottom left"}, recType{"BC", "bottom center"}, recType{"BR", "bottom right"}, } recListBaseline := []recType{ recType{"AL", "baseline left"}, recType{"AC", "baseline center"}, recType{"AR", "baseline right"}, } var formatRect = func(pdf *gofpdf.Fpdf, recList []recType) { linkStr := "" for pageJ := 0; pageJ < 2; pageJ++ { pdf.AddPage() pdf.SetMargins(10, 10, 10) pdf.SetAutoPageBreak(false, 0) borderStr := "1" for _, rec := range recList { pdf.SetXY(20, 20) pdf.CellFormat(170, 257, rec.txt, borderStr, 0, rec.align, false, 0, linkStr) borderStr = "" } linkStr = "https://github.com/jung-kurt/gofpdf" } } pdf := gofpdf.New("P", "mm", "A4", "") // A4 210.0 x 297.0 pdf.SetFont("Helvetica", "", 16) formatRect(pdf, recList) formatRect(pdf, recListBaseline) var fr fontResourceType pdf.SetFontLoader(fr) pdf.AddFont("Calligrapher", "", "calligra.json") pdf.SetFont("Calligrapher", "", 16) formatRect(pdf, recListBaseline) fileStr := exampleFilename("Fpdf_CellFormat_2_align") err := pdf.OutputFileAndClose(fileStr) summary(err, fileStr) // Output: // Generalized font loader reading calligra.json // Generalized font loader reading calligra.z // Successfully generated pdf/Fpdf_CellFormat_2_align.pdf }
// This example demonstrates creating and using templates func ExampleFpdf_CreateTemplate() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.SetCompression(false) // pdf.SetFont("Times", "", 12) template := pdf.CreateTemplate(func(tpl *gofpdf.Tpl) { tpl.Image(imageFile("logo.png"), 6, 6, 30, 0, false, "", 0, "") tpl.SetFont("Arial", "B", 16) tpl.Text(40, 20, "Template says hello") tpl.SetDrawColor(0, 100, 200) tpl.SetLineWidth(2.5) tpl.Line(95, 12, 105, 22) }) _, tplSize := template.Size() // fmt.Println("Size:", tplSize) // fmt.Println("Scaled:", tplSize.ScaleBy(1.5)) template2 := pdf.CreateTemplate(func(tpl *gofpdf.Tpl) { tpl.UseTemplate(template) subtemplate := tpl.CreateTemplate(func(tpl2 *gofpdf.Tpl) { tpl2.Image(imageFile("logo.png"), 6, 86, 30, 0, false, "", 0, "") tpl2.SetFont("Arial", "B", 16) tpl2.Text(40, 100, "Subtemplate says hello") tpl2.SetDrawColor(0, 200, 100) tpl2.SetLineWidth(2.5) tpl2.Line(102, 92, 112, 102) }) tpl.UseTemplate(subtemplate) }) pdf.SetDrawColor(200, 100, 0) pdf.SetLineWidth(2.5) pdf.SetFont("Arial", "B", 16) pdf.AddPage() pdf.UseTemplate(template) pdf.UseTemplateScaled(template, gofpdf.PointType{0, 30}, tplSize) pdf.UseTemplateScaled(template, gofpdf.PointType{0, 60}, tplSize.ScaleBy(1.4)) pdf.Line(40, 210, 60, 210) pdf.Text(40, 200, "Template example page 1") pdf.AddPage() pdf.UseTemplate(template2) pdf.Line(60, 210, 80, 210) pdf.Text(40, 200, "Template example page 2") fileStr := exampleFilename("Fpdf_CreateTemplate") err := pdf.OutputFileAndClose(fileStr) summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_CreateTemplate.pdf }
// This example demonstrates alpha transparency. func ExampleFpdf_SetAlpha() { const ( gapX = 10.0 gapY = 9.0 rectW = 40.0 rectH = 58.0 pageW = 210 pageH = 297 ) modeList := []string{"Normal", "Multiply", "Screen", "Overlay", "Darken", "Lighten", "ColorDodge", "ColorBurn", "HardLight", "SoftLight", "Difference", "Exclusion", "Hue", "Saturation", "Color", "Luminosity"} pdf := gofpdf.New("", "", "", "") pdf.SetLineWidth(2) pdf.SetAutoPageBreak(false, 0) pdf.AddPage() pdf.SetFont("Helvetica", "", 18) pdf.SetXY(0, gapY) pdf.SetTextColor(0, 0, 0) pdf.CellFormat(pageW, gapY, "Alpha Blending Modes", "", 0, "C", false, 0, "") j := 0 y := 3 * gapY for col := 0; col < 4; col++ { x := gapX for row := 0; row < 4; row++ { pdf.Rect(x, y, rectW, rectH, "D") pdf.SetFont("Helvetica", "B", 12) pdf.SetFillColor(0, 0, 0) pdf.SetTextColor(250, 250, 230) pdf.SetXY(x, y+rectH-4) pdf.CellFormat(rectW, 5, modeList[j], "", 0, "C", true, 0, "") pdf.SetFont("Helvetica", "I", 150) pdf.SetTextColor(80, 80, 120) pdf.SetXY(x, y+2) pdf.CellFormat(rectW, rectH, "A", "", 0, "C", false, 0, "") pdf.SetAlpha(0.5, modeList[j]) pdf.Image(imageFile("golang-gopher.png"), x-gapX, y, rectW+2*gapX, 0, false, "", 0, "") pdf.SetAlpha(1.0, "Normal") x += rectW + gapX j++ } y += rectH + gapY } fileStr := exampleFilename("Fpdf_SetAlpha_transparency") err := pdf.OutputFileAndClose(fileStr) summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_SetAlpha_transparency.pdf }
// This examples tests corner cases as reported by the gocov tool. func ExampleFpdf_SetKeywords() { var err error fileStr := exampleFilename("Fpdf_SetKeywords") err = gofpdf.MakeFont(fontFile("CalligrapherRegular.pfb"), fontFile("cp1252.map"), cnFontDir, nil, true) if err == nil { err = gofpdf.MakeFont(fontFile("calligra.ttf"), fontFile("cp1252.map"), cnFontDir, nil, true) if err == nil { pdf := gofpdf.New("", "", "", "") pdf.SetFontLocation(cnFontDir) pdf.SetTitle("世界", true) pdf.SetAuthor("世界", true) pdf.SetSubject("世界", true) pdf.SetCreator("世界", true) pdf.SetKeywords("世界", true) pdf.AddFont("Calligrapher", "", "CalligrapherRegular.json") pdf.AddPage() pdf.SetFont("Calligrapher", "", 16) pdf.Writef(5, "\x95 %s \x95", pdf) err = pdf.OutputFileAndClose(fileStr) } } summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_SetKeywords.pdf }