Exemplo n.º 1
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")
}
Exemplo n.º 2
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")
}
Exemplo n.º 3
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")

}
Exemplo n.º 4
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")
}
Exemplo 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")
}
Exemplo n.º 6
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")

}
Exemplo n.º 7
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
	}
}