Golang如何在PDF文件中写入表格而非渲染

Golang如何在PDF文件中写入表格而非渲染 示例

±---------------±-----------±----------------±-----------±-----------±-------------±-----------±---------------±-------------+
|  CLUSTER NAME  | CLUSTER ID | DATACENTER NAME | RPOOL NAME |  RPOOL ID  | NETWORK NAME | NETWORK ID | DATASTORE NAME | DATASTORE ID |
±---------------±-----------±----------------±-----------±-----------±-------------±-----------±---------------±-------------+
| ONPREM_CLUSTER | domain-c7  | ONPREM_DC       | Resources  | resgroup-8 | VM Network   | network-12 | datastore1     | datastore-11 |
|                |            |                 |            |            |              |            | ONPREM_DS      | datastore-14 |
|                |            |                 |            |            |              |            |                |              |
±---------------±-----------±----------------±-----------±-----------±-------------±-----------±---------------±-------------+

更多关于Golang如何在PDF文件中写入表格而非渲染的实战教程也可以访问 https://www.itying.com/category-94-b0.html

3 回复

你好

此链接可用于将HTML渲染为PDF的示例。同一页面还有其他相关示例:创建PDF的Golang方法

更多关于Golang如何在PDF文件中写入表格而非渲染的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


我的markdown转PDF工具支持markdown格式的表格:

GitHub GitHub

头像

mandolyte/mdtopdf

Markdown转PDF。通过在GitHub上创建账户来为mandolyte/mdtopdf开发做贡献。

在Go语言中,要在PDF文件中写入表格而不是渲染为图形,可以使用支持文本表格的PDF库。推荐使用github.com/jung-kurt/gofpdf库,它能够创建包含表格的PDF文档。

以下是一个示例代码,展示如何创建包含表格的PDF文件:

package main

import (
	"github.com/jung-kurt/gofpdf"
)

func main() {
	pdf := gofpdf.New("L", "mm", "A4", "")
	pdf.AddPage()
	
	// 设置字体
	pdf.SetFont("Arial", "B", 12)
	
	// 表头数据
	headers := []string{"CLUSTER NAME", "CLUSTER ID", "DATACENTER NAME", "RPOOL NAME", 
		"RPOOL ID", "NETWORK NAME", "NETWORK ID", "DATASTORE NAME", "DATASTORE ID"}
	
	// 表格数据
	data := [][]string{
		{"ONPREM_CLUSTER", "domain-c7", "ONPREM_DC", "Resources", "resgroup-8", 
			"VM Network", "network-12", "datastore1", "datastore-11"},
		{"", "", "", "", "", "", "", "ONPREM_DS", "datastore-14"},
		{"", "", "", "", "", "", "", "", ""},
	}
	
	// 设置列宽(根据内容调整)
	colWidth := 30.0
	lineHeight := 6.0
	
	// 绘制表头
	pdf.SetFillColor(200, 200, 200)
	for _, header := range headers {
		pdf.CellFormat(colWidth, lineHeight, header, "1", 0, "C", true, 0, "")
	}
	pdf.Ln(-1)
	
	// 绘制表格内容
	pdf.SetFillColor(255, 255, 255)
	for _, row := range data {
		for _, cell := range row {
			pdf.CellFormat(colWidth, lineHeight, cell, "1", 0, "C", false, 0, "")
		}
		pdf.Ln(-1)
	}
	
	// 保存PDF文件
	err := pdf.OutputFileAndClose("table.pdf")
	if err != nil {
		panic(err)
	}
}

如果需要更复杂的表格样式,可以使用github.com/signintech/gopdf库:

package main

import (
	"github.com/signintech/gopdf"
)

func main() {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4Landscape})
	pdf.AddPage()
	
	// 设置字体
	err := pdf.AddTTFFont("arial", "./arial.ttf")
	if err != nil {
		panic(err)
	}
	
	err = pdf.SetFont("arial", "", 10)
	if err != nil {
		panic(err)
	}
	
	// 表头
	headers := []string{"CLUSTER NAME", "CLUSTER ID", "DATACENTER NAME", "RPOOL NAME", 
		"RPOOL ID", "NETWORK NAME", "NETWORK ID", "DATASTORE NAME", "DATASTORE ID"}
	
	// 表格数据
	data := [][]string{
		{"ONPREM_CLUSTER", "domain-c7", "ONPREM_DC", "Resources", "resgroup-8", 
			"VM Network", "network-12", "datastore1", "datastore-11"},
		{"", "", "", "", "", "", "", "ONPREM_DS", "datastore-14"},
		{"", "", "", "", "", "", "", "", ""},
	}
	
	// 绘制表格
	x := 50.0
	y := 50.0
	colWidth := 60.0
	rowHeight := 20.0
	
	// 绘制表头
	for i, header := range headers {
		pdf.SetX(x + float64(i)*colWidth)
		pdf.SetY(y)
		pdf.Cell(nil, header)
	}
	
	// 绘制表格内容
	for rowIdx, row := range data {
		for colIdx, cell := range row {
			pdf.SetX(x + float64(colIdx)*colWidth)
			pdf.SetY(y + float64(rowIdx+1)*rowHeight)
			pdf.Cell(nil, cell)
		}
	}
	
	// 保存文件
	pdf.WritePdf("table_gopdf.pdf")
}

首先安装所需的库:

go get github.com/jung-kurt/gofpdf
go get github.com/signintech/gopdf

这些代码会创建包含文本表格的PDF文件,表格内容是可选择和搜索的文本,而不是渲染的图像。

回到顶部