Home > Article > Backend Development > How to use template functions in Go language to dynamically generate Word documents and export PDF?
How to use template functions in Go language to dynamically generate Word documents and export PDF?
Introduction:
During the development process, we often need to generate Word documents based on templates and export them to PDF. This article will introduce how to dynamically generate and export PDF documents through template function examples in the Go language.
1. Install the required libraries and tools
Before we start, we need to install and configure the following libraries and tools:
2. Create a Word template file
Before we start, we need to create a Word template file (.docx format) and define the content that needs to be dynamically generated. Markers can be included in the template so that we can replace them using template functions in subsequent steps.
The following is the content of a simple example Word template file:
欢迎您,{{.Name}}! 以下是您的订单详情: 订单号:{{.OrderNumber}} 订单总价:{{.TotalPrice}}
Please note that {{.Name}}, {{.OrderNumber}} and {{.TotalPrice}} are tags , we will use template functions to replace them with dynamic data.
3. Use template functions to generate Word documents
First, we need to import the required packages and libraries:
package main import ( "fmt" "os" "text/template" )
Then, we define a structure to save dynamic data:
type Order struct { Name string OrderNumber string TotalPrice float64 }
Next, we use the template function to generate a Word document:
func main() { // 定义模板文件路径 tmpl := "./template.docx" // 定义动态数据 order := Order{ Name: "张三", OrderNumber: "2021123456789", TotalPrice: 100.00, } // 解析模板文件 t := template.Must(template.ParseFiles(tmpl)) // 生成Word文档 docxFile, err := os.Create("./output.docx") if err != nil { fmt.Println("创建Word文档失败:", err) return } defer docxFile.Close() // 渲染模板并写入Word文档 err = t.Execute(docxFile, order) if err != nil { fmt.Println("生成Word文档失败:", err) return } fmt.Println("Word文档生成成功!") }
4. Export the Word document to PDF
To export the generated Word document to PDF, we need to use the Go-PDF library .
First, we need to import the required packages and libraries:
package main import ( "fmt" "github.com/signintech/gopdf" )
Then, we use the following code to convert the Word document to PDF:
func main() { // 打开生成的Word文档 docxFile, err := os.Open("./output.docx") if err != nil { fmt.Println("打开Word文档失败:", err) return } defer docxFile.Close() // 创建PDF文档 pdf := gopdf.GoPdf{} pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4}) pdf.AddPage() // 将Word文档内容添加到PDF中 err = pdf.AddTTFFont("font", "./arialuni.ttf") if err != nil { fmt.Println("添加字体失败:", err) return } pdf.SetFont("font", "", 14) pdf.SetX(40) pdf.SetY(40) // 读取Word文档内容并写入PDF bs, _ := ioutil.ReadAll(docxFile) pdf.Cell(nil, string(bs)) // 保存为PDF文件 pdf.WritePdf("./output.pdf") fmt.Println("PDF文件导出成功!") }
Please note that the above code arialuni.ttf used in is a Unicode font file used to support the display of Chinese characters. You need to download the font file and save it in the root directory of your project.
5. Test run
After completing the above steps, we use the go run command to run the code. After successful operation, two files, output.docx and output.pdf, will be generated in the project root directory.
Please note that the generated output.docx is a dynamically generated Word document, and output.pdf is the result of exporting the Word document to PDF.
Conclusion:
This article introduces how to use the template function in the Go language to dynamically generate Word documents and export them to PDF. This approach is very flexible and can meet a variety of dynamic generation and export needs. I hope this article will help you generate Word documents and export PDF in Go language development.
The above is the detailed content of How to use template functions in Go language to dynamically generate Word documents and export PDF?. For more information, please follow other related articles on the PHP Chinese website!