如何通过gopdf将文本数据打印成pdf?

我尝试了什么:


var data string

data = "Line1 <br>\n" +

       "Line2 \n" +

       "Line3 \n" +

       "Line4" // It's got from DB which saved by br data.


pdf.SetFont("wts11", "", 14)

pdf.SetX(100)

pdf.SetY(200)

pdf.Text(data)

结果:


pdf显示:

http://img1.sycdn.imooc.com/648ff24b000175c106500086.jpg

所有其他打印的数据都消失了。

也许它无法解析<br>

即使改成pdf.Text(data)pdf.Cell(nil, text)也是一样的结果。


胡子哥哥
浏览 136回答 1
1回答

翻阅古今

不要跳过错误处理,这就是为什么你看不到失败的原因。请将错误检查添加到SetFont, Text,WritePdf功能。就我而言,我发现错误来自SetFont于未找到的错误。open-sans.ttf如果您的工作目录中有此代码,则此代码效果很好:package mainimport (&nbsp; &nbsp; "log"&nbsp; &nbsp; "github.com/signintech/gopdf")func main() {&nbsp; &nbsp; pdf := gopdf.GoPdf{}&nbsp; &nbsp; pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})&nbsp; &nbsp; pdf.AddPage()&nbsp; &nbsp; if err := pdf.AddTTFFont("open-sans", "open-sans.ttf"); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Print(err.Error())&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; if err := pdf.SetFont("open-sans", "", 14); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Print(err.Error())&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; pdf.SetX(100)&nbsp; &nbsp; pdf.SetY(200)&nbsp; &nbsp; data := "Line1 <br/>\n" +&nbsp; &nbsp; &nbsp; &nbsp; "Line2 \n" +&nbsp; &nbsp; &nbsp; &nbsp; "Line3 \n" +&nbsp; &nbsp; &nbsp; &nbsp; "Line4"&nbsp; &nbsp; if err := pdf.Text(data); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Print(err.Error())&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; if err := pdf.WritePdf("hello.pdf"); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Print(err.Error())&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go