Go PDF文件操作

目录

介绍

[安装 gofpdf](#安装 gofpdf)

API

代码示例

结果展示


介绍

gofpdf 是一个在 Go 语言中用于生成 PDF 文档的库。

安装 gofpdf

首先,你需要安装 gofpdf 库。你可以使用 go get 命令来安装它:

bash 复制代码
	go get github.com/jung-kurt/gofpdf

API

功能 函数名 参数解释 示例
创建PDF对象 gofpdf.New(orientation, unit, size, fontDir) - orientation: 页面方向("P"为纵向, "L"为横向)<br>- unit: 单位("pt", "mm", "cm", "in")<br>- size: 纸张大小(如"A4")<br>- fontDir: 字体目录(可选) pdf := gofpdf.New("P", "mm", "A4", "")
添加页面 AddPage() 无参数 pdf.AddPage()
设置字体 SetFont(family, style, size) - family: 字体名称<br>- style: 字体样式("", "B", "I", "U")<br>- size: 字体大小 pdf.SetFont("Arial", "B", 16)
输出文本 Cell(width, height, txt, border, ln, align, fill, link, linkStr, x, y, res, automaticWidth, autoPageBreak) - width: 单元格宽度<br>- height: 单元格高度<br>- txt: 文本内容<br>- 其他参数为可选,用于设置边框、换行、对齐方式等 pdf.Cell(40, 10, "Hello, World!")
输出多行文本 MultiCell(width, cellHeight, txt, border, align, fill, ln, x, y, res, automaticWidth, autoPageBreak) 类似Cell,但支持多行文本 pdf.MultiCell(40, 10, "Multiple lines of text", 0, "C")
绘制线条 Line(x1, y1, x2, y2) - x1, y1: 起点坐标<br>- x2, y2: 终点坐标 pdf.Line(10, 10, 100, 100)
绘制矩形 Rect(x, y, width, height, style) - x, y: 左上角坐标<br>- width, height: 矩形宽和高<br>- style: 边框样式 pdf.Rect(50, 50, 100, 50, "D")
添加图片 Image(imageFile, x, y, width, height, imageType, link, linkStr) - imageFile: 图片文件路径<br>- x, y: 图片位置<br>- width, height: 图片尺寸<br>- 其他参数为可选 pdf.Image("path/to/image.jpg", 10, 10, 30, 0, false, "", 0, "")
设置文本颜色 SetTextColor(r, g, b) - r, g, b: RGB颜色值 pdf.SetTextColor(255, 0, 0)
设置绘图颜色 SetDrawColor(r, g, b) - r, g, b: RGB颜色值 pdf.SetDrawColor(0, 0, 255)
设置填充颜色 SetFillColor(r, g, b) - r, g, b: RGB颜色值 pdf.SetFillColor(255, 255, 0)
输出文件 OutputFileAndClose(filename) - filename: 输出文件名 err := pdf.OutputFileAndClose("output.pdf")

代码示例

注意:图片需要自行准备否则运行不起来!!!

Go 复制代码
package main

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

func main() {
	// 创建一个新的PDF文档
	pdf := gofpdf.New("P", "mm", "A4", "")

	// 添加第一页
	pdf.AddPage()

	// 设置字体和大小
	pdf.SetFont("Arial", "", 14)

	// 输出文本
	pdf.Cell(40, 10, "Hello, World!")

	// 添加一些空行
	pdf.Ln(10)

	// 输出多行文本
	txt := "This is a multi-line text example.\nEach line is separated by a newline character."
	pdf.MultiCell(100, 5, txt, "1", "J", false)

	// 添加一个矩形
	pdf.SetDrawColor(255, 0, 0)    // 设置线条颜色为红色
	pdf.Rect(50, 50, 100, 50, "D") // 画一个红色矩形,带边框("D"表示绘制线条)

	// 添加一个填充矩形
	pdf.SetFillColor(0, 255, 0)     // 设置填充颜色为绿色
	pdf.Rect(50, 120, 100, 50, "F") // 画一个绿色矩形,填充("F"表示填充)

	// 绘制一条线
	pdf.SetDrawColor(0, 0, 255) // 设置线条颜色为蓝色
	pdf.Line(10, 200, 200, 200) // 从(10,200)到(200,200)画一条蓝色线

	// 添加图片(确保图片路径是正确的)
	pdf.Image("E:\\SystemFiles\\Pictures\\go-logo.png", 160, 220, 30, 0, false, "", 0, "")

	// 设置文本颜色并输出
	pdf.SetTextColor(255, 0, 0) // 设置为红色
	pdf.Cell(40, 10, "Text in red color")

	// 输出PDF到文件
	err := pdf.OutputFileAndClose("example.pdf")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println("PDF file created successfully!")
}

结果展示

相关推荐
大强同学19 小时前
Adobe DC 2025安装教程
windows·adobe·pdf
诸葛大钢铁1 天前
如何免费在线PDF转换成Excel
职场和发展·pdf·excel
开开心心就好1 天前
Word图片格式调整与转换工具
java·javascript·spring·eclipse·pdf·word·excel
icloudelectron1 天前
Altium Designer AD如何输出PIN带网络名的PDF装配图
pdf
沉到海底去吧Go1 天前
【软件工具】基于PDF文件内容识别的改名软件,PDF根据内容自动重命名,如何识别pdf内容并做文件命名,PDF批量改名
pdf·扫描pdf文档批量文件改名·批量提取识别pdf中的特定字段·根据pdf某个区域内容改名·图片识别工具
EchoZeal1 天前
【实测有效】Edge浏览器打开部分pdf文件显示空白
edge·pdf·adobe acrobat
敲代码的小吉米2 天前
前端上传el-upload、原生input本地文件pdf格式(纯前端预览本地文件不走后端接口)
前端·javascript·pdf·状态模式
东方巴黎~Sunsiny2 天前
EasyExcel导出excel再转PDF转图片详解
pdf·excel
aklry2 天前
uniapp实现在线pdf预览以及下载
前端·pdf·uni-app
繁依Fanyi2 天前
我的 PDF 工具箱:CodeBuddy 打造 PDFMagician 的全过程记录
java·pdf·uni-app·生活·harmonyos·codebuddy首席试玩官