Beispiel #1
0
// Page size example
func ExampleFpdf_tutorial15() {
	pdf := gofpdf.NewCustom(&gofpdf.InitType{
		UnitStr:    "in",
		Size:       gofpdf.SizeType{Wd: 6, Ht: 6},
		FontDirStr: cnFontDir,
	})
	pdf.SetMargins(0.5, 1, 0.5)
	pdf.SetFont("Times", "", 14)
	pdf.AddPageFormat("L", gofpdf.SizeType{Wd: 3, Ht: 12})
	pdf.SetXY(0.5, 1.5)
	pdf.CellFormat(11, 0.2, "12 in x 3 in", "", 0, "C", false, 0, "")
	pdf.AddPage() // Default size established in NewCustom()
	pdf.SetXY(0.5, 3)
	pdf.CellFormat(5, 0.2, "6 in x 6 in", "", 0, "C", false, 0, "")
	pdf.AddPageFormat("P", gofpdf.SizeType{Wd: 3, Ht: 12})
	pdf.SetXY(0.5, 6)
	pdf.CellFormat(2, 0.2, "3 in x 12 in", "", 0, "C", false, 0, "")
	for j := 0; j <= 3; j++ {
		wd, ht, u := pdf.PageSize(j)
		fmt.Printf("%d: %6.2f %s, %6.2f %s\n", j, wd, u, ht, u)
	}
	pdf.OutputAndClose(docWriter(pdf, 15))
	// Output:
	// 0:   6.00 in,   6.00 in
	// 1:  12.00 in,   3.00 in
	// 2:   6.00 in,   6.00 in
	// 3:   3.00 in,  12.00 in
	// Successfully generated pdf/tutorial15.pdf
}
Beispiel #2
0
func Barcode(data string) (pdf *gofpdf.Fpdf, err error) {
	b, err := code128.Encode(data)
	if err != nil {
		return
	}

	b, err = barcode.Scale(b, b.Bounds().Size().X, 10)
	if err != nil {
		panic(err)
	}

	out, err := ioutil.TempFile("", "lblmgrpnpbarcode")
	if err != nil {
		panic(err)
	}
	var opt jpeg.Options

	opt.Quality = 100
	err = jpeg.Encode(out, b, &opt)
	if err != nil {
		return
	}

	imgHeight := float64(b.Bounds().Size().Y)
	imgWidth := float64(b.Bounds().Size().X / 3)

	pdf = gofpdf.NewCustom(&gofpdf.InitType{
		UnitStr: "mm",
		Size:    gofpdf.SizeType{Wd: imgWidth, Ht: imgHeight},
	})

	pdf.AddPage()

	height, width := pdf.GetPageSize()
	pdf.Image(out.Name(), 0, 0, height, width, false, "jpg", 0, "")

	pdf.SetFillColor(255, 255, 255)
	pdf.Rect(0, 7, imgWidth, 5, "F")
	pdf.SetFont("Arial", "B", 6)
	pdf.Text(0, 9.1, data)
	out.Close()
	os.Remove(out.Name())
	return
}
Beispiel #3
0
// NewGoFpdf generates a new layout based on a Layout interface and uses the
// jung-kurt/gofpdf library to process all the information.
func NewGoFpdf(l layouts.Layout) (pdf *GoFpdf) {
	init := &gofpdf.InitType{
		OrientationStr: l.Orientation(),
		UnitStr:        l.Unit(),
		Size:           gofpdf.SizeType{Wd: l.Width(), Ht: l.Height()},
		FontDirStr:     "./fonts",
	}
	fpdf := gofpdf.NewCustom(init)
	fpdf.SetMargins(0, 0, 0)
	fpdf.AddPage()
	fpdf.SetAutoPageBreak(true, 0)

	pdf = new(GoFpdf)
	pdf.fpdf = fpdf
	pdf.layout = l

	for fontName, fontStyles := range l.Fonts() {
		pdf.LoadFonts(fontName, fontStyles)
	}

	return
}