Ejemplo n.º 1
0
//gopdf had no mechanism for set italic ( pdf.SetFont("SOMEFONT", "I", 14) ), but you can do like this.
func main() {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{Unit: "pt", PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
	pdf.AddPage()

	err := pdf.AddTTFFont("DejaVuSerif", "../ttf/DejaVuSerif.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}
	err = pdf.AddTTFFont("DejaVuSerif-Italic", "../ttf/DejaVuSerif-Italic.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}

	err = pdf.SetFont("DejaVuSerif", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.Cell(nil, "Hi! This is nomal.")
	pdf.Br(20)

	err = pdf.SetFont("DejaVuSerif-Italic", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.Cell(nil, "Hi! This is italic.")
	pdf.WritePdf("italic.pdf")
}
Ejemplo n.º 2
0
func main() {

	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
	pdf.AddPage()
	var err error
	err = pdf.AddTTFFont("HDZB_5", "../ttf/wts11.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}

	err = pdf.AddTTFFont("TakaoPGothic", "../ttf/TakaoPGothic.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}

	err = pdf.AddTTFFont("loma", "../ttf/Loma.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}

	err = pdf.AddTTFFont("namum", "../ttf/NanumBarunGothic.ttf")

	//china
	err = pdf.SetFont("HDZB_5", "", 14)
	if err != nil {
		log.Printf(err.Error())
		return
	}

	err = pdf.AddTTFFont("Roboto", "../ttf/Roboto-Regular.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}

	pdf.Cell(nil, "Hello")
	pdf.Br(20)
	pdf.Cell(nil, "您好")
	pdf.Br(20)

	//japan
	err = pdf.SetFont("TakaoPGothic", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.Cell(nil, "こんにちは")
	pdf.Br(20)

	//thai
	err = pdf.SetFont("loma", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.Cell(nil, "สวัสดี")
	pdf.Br(20)

	//test composite glyph
	err = pdf.SetFont("Roboto", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.Cell(nil, "ÁÉÍÓÖŐÚÜŰ")
	pdf.Br(20)
	pdf.Cell(nil, "ÁÉÍÓÖŐÚÜŰ")
	pdf.Br(20)

	//korean
	err = pdf.SetFont("namum", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.Cell(nil, "안녕하세요")
	pdf.Br(20)

	pdf.WritePdf("hello2.pdf")

}
Ejemplo n.º 3
0
func main() {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{Unit: "mm", PageSize: gopdf.Rect{W: 210.0, H: 297.0}}) //595.28pt, 841.89pt = A4
	pdf.AddPage()
	err := pdf.AddTTFFont("DROID", "DroidSerif-Regular.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}

	err = pdf.SetFont("DROID", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.Cell(nil, "Hello world!")
	pdf.SetY(30.0)
	pdf.Cell(nil, "Привет мир!")

	pdf.SetGrayStroke(0.5)
	pdf.Oval(10, 200, 200, 250)

	pdf.WritePdf("hello.pdf")
}
Ejemplo n.º 4
0
/*
Thank https://github.com/douglasm for write setgray .
*/
func main() {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
	pdf.AddPage()
	err := pdf.AddTTFFont("HDZB_5", "../ttf/wts11.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}
	err = pdf.SetFont("HDZB_5", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.SetGrayFill(0.5)
	pdf.Cell(nil, "您好")
	pdf.WritePdf("hello.pdf")
	//test
}
Ejemplo n.º 5
0
/*
Thank https://github.com/miiton for write text color supported.
*/
func main() {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
	pdf.AddPage()
	err := pdf.AddTTFFont("HDZB_5", "../ttf/wts11.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}
	err = pdf.SetFont("HDZB_5", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	//
	pdf.SetGrayFill(0.5)
	pdf.Cell(nil, "gray")

	pdf.SetTextColor(255, 0, 0)
	pdf.Br(20)
	pdf.Cell(nil, "red")

	pdf.SetTextColor(0, 0, 0)
	pdf.Br(20)
	pdf.Cell(nil, "gray")

	pdf.SetGrayFill(0)
	pdf.Br(20)
	pdf.Cell(nil, "black")

	pdf.SetTextColor(0, 0, 255)
	pdf.Br(20)
	pdf.Cell(nil, "blue")

	pdf.WritePdf("color.pdf")
}
Ejemplo n.º 6
0
func TestHello(t *testing.T) {

	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{Unit: "pt", PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
	pdf.AddPage()
	err := pdf.AddTTFFont("HDZB_5", "./ttf/wts11.ttf")
	if err != nil {
		t.Error(err)
		return
	}

	err = pdf.SetFont("HDZB_5", "", 14)
	if err != nil {
		t.Error(err)
		return
	}

	pdf.SetGrayFill(0.5)
	pdf.Cell(nil, "您好")
	data, err := pdf.GetBytesPdfReturnErr()
	if err != nil {
		t.Error(err)
		return
	}

	if len(data) <= 0 {
		t.Error(err)
		return
	}

}
Ejemplo n.º 7
0
/*
Thank https://github.com/miiton for write draw oval.
*/
func main() {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
	pdf.AddPage()
	pdf.SetLineWidth(1)
	pdf.Oval(100, 200, 500, 500)
	pdf.WritePdf("oval.pdf")
	//fmt.Printf("")

}
Ejemplo n.º 8
0
func main() {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
	pdf.AddPage()
	err := pdf.AddTTFFont("Roboto", "../ttf/Roboto-Regular.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}
	fontSize := 24
	err = pdf.SetFont("Roboto", "", fontSize)
	if err != nil {
		log.Print(err.Error())
		return
	}
	//pdf.SetGrayFill(0.5)
	//pdf.Cell(nil, "Áa")

	//Measure Width
	text := "How can I cordinate the text that I want draw?"
	pdf.Cell(nil, text)

	realWidth, _ := pdf.MeasureTextWidth(text)
	fmt.Printf("realWidth = %f", realWidth)

	var parser core.TTFParser
	err = parser.Parse("../ttf/Roboto-Regular.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}

	//Measure Height
	//get  CapHeight (https://en.wikipedia.org/wiki/Cap_height)
	cap := float64(float64(parser.CapHeight()) * 1000.00 / float64(parser.UnitsPerEm()))
	//convert
	realHeight := cap * (float64(fontSize) / 1000.0)
	fmt.Printf("realHeight = %f", realHeight)
	//test
	pdf.Br(realHeight)
	pdf.Cell(nil, "How can I cordinate the text that I want draw?")
	pdf.Br(realHeight)
	pdf.Cell(nil, "How can I cordinate the text that I want draw?123")
	pdf.Br(realHeight)
	pdf.Cell(nil, "How can I cordinate the text that I want draw?456")

	pdf.WritePdf("hello.pdf")
}
Ejemplo n.º 9
0
func TestHello2(t *testing.T) {

	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{Unit: "pt", PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
	pdf.AddPage()
	var err error
	err = pdf.AddTTFFont("HDZB_5", "./ttf/wts11.ttf")
	if err != nil {
		t.Error(err)
		return
	}

	err = pdf.AddTTFFont("TakaoPGothic", "./ttf/TakaoPGothic.ttf")
	if err != nil {
		t.Error(err)
		return
	}

	err = pdf.AddTTFFont("loma", "./ttf/Loma.ttf")
	if err != nil {
		t.Error(err)
		return
	}

	err = pdf.AddTTFFont("namum", "./ttf/NanumBarunGothic.ttf")

	//china
	err = pdf.SetFont("HDZB_5", "", 14)
	if err != nil {
		t.Error(err)
		return
	}
	pdf.Cell(nil, "Hello")
	pdf.Br(20)
	pdf.Cell(nil, "您好")
	pdf.Br(20)

	//japan
	err = pdf.SetFont("TakaoPGothic", "", 14)
	if err != nil {
		t.Error(err)
		return
	}
	pdf.Cell(nil, "こんにちは")
	pdf.Br(20)

	//thai
	err = pdf.SetFont("loma", "", 14)
	if err != nil {
		t.Error(err)
		return
	}
	pdf.Cell(nil, "สวัสดี")
	pdf.Br(20)

	//korean
	err = pdf.SetFont("namum", "", 14)
	if err != nil {
		t.Error(err)
		return
	}
	pdf.Cell(nil, "안녕하세요")

	data, err := pdf.GetBytesPdfReturnErr()
	if err != nil {
		t.Error(err)
		return
	}

	if len(data) <= 0 {
		t.Error(err)
		return
	}
}
Ejemplo n.º 10
0
func main() {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
	pdf.AddPage()
	err := pdf.AddTTFFont("Roboto", "../ttf/Roboto-Regular.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}
	fontSize := 14
	err = pdf.SetFont("Roboto", "", fontSize)
	if err != nil {
		log.Print(err.Error())
		return
	}
	//pdf.SetGrayFill(0.5)
	pdf.Cell(nil, "ÁÉÍÓÖŐÚÜŰ")

	pdf.WritePdf("hello.pdf")
}
Ejemplo n.º 11
0
func main() {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
	pdf.AddPage()
	var err error
	err = pdf.AddTTFFont("loma", "../ttf/Loma.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}

	//#1 pic
	pdf.Image("../imgs/gopher.jpg", 200, 50, nil)
	err = pdf.SetFont("loma", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.SetX(250)
	pdf.SetY(200)
	pdf.Cell(nil, "gopher and gopher")

	pdf.WritePdf("image.pdf")
}
Ejemplo n.º 12
0
/*
Thank https://github.com/miiton for setLineType
*/
func main() {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{Unit: "pt", PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
	pdf.AddPage()
	err := pdf.AddTTFFont("TakaoPGothic", "../ttf/TakaoPGothic.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.SetLineWidth(2)
	pdf.SetLineType("dashed")
	//pdf.SetLineType("dotted")
	pdf.Line(10, 30, 585, 30)

	err = pdf.SetFont("TakaoPGothic", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.Cell(nil, "こんにちは")
	pdf.Br(20)
	pdf.WritePdf("line.pdf")

}
Ejemplo n.º 13
0
func main() {
	pdf := gopdf.GoPdf{}

	pdf.Start(gopdf.Config{
		PageSize: gopdf.Rect{W: 595.28, H: 841.89}, //595.28, 841.89 = A4
		Protection: gopdf.PDFProtectionConfig{UseProtection: true,
			Permissions: gopdf.PermissionsPrint | gopdf.PermissionsCopy | gopdf.PermissionsModify,
			OwnerPass:   []byte("1234"),
			UserPass:    []byte("5555")},
	})

	pdf.AddPage()
	err := pdf.AddTTFFont("TakaoPGothic", "../ttf/TakaoPGothic.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.SetLineWidth(2)
	pdf.SetLineType("dashed")
	pdf.SetLineType("dotted")
	pdf.Line(10, 30, 585, 30)
	err = pdf.SetFont("TakaoPGothic", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}

	//#1 pic
	pdf.Image("../imgs/gopher.jpg", 200, 50, nil)

	pdf.Cell(nil, "こんにちは")
	pdf.Br(20)
	pdf.WritePdf("protect.pdf")
}
Ejemplo n.º 14
0
func main() {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
	pdf.AddPage()
	err := pdf.AddTTFFont("TakaoPGothic", "../ttf/TakaoPGothic.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}

	err = pdf.SetFont("TakaoPGothic", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.SetX(0)
	text01 := "こんにちは"
	pdf.Cell(nil, text01)
	w01, _ := pdf.MeasureTextWidth(text01)
	pdf.SetY(20)
	pdf.SetX(w01)

	text02 := "i am a man."
	pdf.Cell(nil, text02)
	w02, _ := pdf.MeasureTextWidth(text02)
	pdf.SetY(30)
	pdf.SetX(w01 + w02)

	text03 := "done"
	pdf.Cell(nil, text03)

	fmt.Printf("MeasureTextWidth = %f\n", w01)
	pdf.WritePdf("m.pdf")
}