首页 > 解决方案 > gofpdf - 居中对齐文本之间的粗体文本

问题描述

我想在居中对齐的文本之间呈现文本。这是我尝试过的

html := pdf.HTMLBasicNew()
_, lineHt := pdf.GetFontSize()
html.Write(lineHt, "<center>My name is <b>Bot<b></center>")

但仅My name is与中心对齐而不是整个文本My name is Bot

如何使用gofpdf 或 go 中的任何其他包来实现这一点?

标签: gopdf-generation

解决方案


我创建了一个用于生成 pdf 的简单程序并将您的代码片段插入其中,但我没有遇到任何问题,整个文本My name is Bot都与中心对齐。以下是我编写的代码:

package main

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

func main() {
    pdf := gofpdf.New("P", "mm", "A4", "")
    pdf.AddPage()
    pdf.SetFont("Courier", "", 16)

    html := pdf.HTMLBasicNew()
    _, lineHt := pdf.GetFontSize()
    html.Write(lineHt, "<center>My name is <b>Bot<b></center>")
    err := pdf.OutputFileAndClose("myFile2.pdf")
    if err != nil {
        panic(err)
    }
}

推荐阅读