登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> SoftHub关联区 >> 主题: [word/excel/pdf]golang 操作 pdf -- signintech/gopdf     [回主站]     [分站链接]
[word/excel/pdf]golang 操作 pdf -- signintech/gopdf
clq
浏览(264) - 2020-10-29 17:26:33 发表 编辑

关键字:

[2020-10-29 20:10:48 最后更新]
golang 操作 pdf -- signintech/gopdf

https://studygolang.com/articles/11398

----
signintech/gopdf

既然jung-kurt/gofpdf不支持中文,那么就介绍一个支持中文的signintech/gopdf。

github地址:
https://github.com/signintech/gopdf

Star: 422

获取:

go get -u github.com/signintech/gopdf

生成PDF文件

为了炫酷一点,自己从网上下载一个字体。

package main

import (
"log"

"github.com/signintech/gopdf"
)

func main() {

pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
pdf.AddPage()
err := pdf.AddTTFFont("wts11", "TTENuoJ_0.ttf")
if err != nil {
log.Print(err.Error())
return
}

err = pdf.SetFont("wts11", "", 14)
if err != nil {
log.Print(err.Error())
return
}
pdf.Cell(nil, "我闭目在经殿的香雾中, 蓦然听见你颂经中的真言;")
pdf.WritePdf("hello.pdf")

}

----------------------------------------------------------------
另外,它们应该都没有 npoi,poi 成熟。

---
c#开源Excel操作库--NPOI
前言

以前也用C#操作过excel,用的是OleDb或者offic的com组件,但是总是非常的麻烦,依赖限制较多,所以果断寻找开源方案,JAVA上面已经有非常成熟的POI,就这样,找到了移.Net的移植版--NPOI,这样就可以不用依赖任何东西,只用这个库即可。
介绍

开源协议:Apache 2.0

使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写。NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office的情况下对Word/Excel文档进行读写操作。

---------------------
Excelize 是 Golang 编写的一个用来操作 Office Excel 文档类库,基于微软的 Office OpenXML 标准。可以使用它来读取、写入 XLSX 文件。相比较其他的开源类库,Excelize 支持写入原本带有图片(表)的文档,还支持向 Excel 中插入图片,并且在保存后不会丢失图表样式。
安装

go get github.com/Luxurioust/excelize

----------------------------------------------------------------
另外,还有一个库已经证实是已经被收购了的。
"github.com/unidoc/unioffice/document" //不行,会提示 Unlicensed version of UniOffice
//似乎是收购的 "baliance.com/gooxml/document"


clq  2020-10-29 17:27:36 发表 编辑


Go实战--golang中操作PDF(rsc.io/pdf、jung-kurt/gofpdf、signintech/gopdf)
wangshubo1989 · 2017-10-18 15:01:21 · 4188 次点击 · 预计阅读时间 5 分钟 · 20分钟之前 开始浏览
这是一个创建于 2017-10-18 15:01:21 的文章,其中的信息可能已经有所发展或是发生改变。

生命不止,继续 go go go !!!

昨天介绍了golang中如何操作excel:
Go实战–golang中操作excel(tealeg/xlsx、360EntSecGroup-Skylar/excelize)

那么今天就跟大家分享一下,golang中如何操作PDF。
PDF简介

The Portable Document Format (PDF) is a file format used to present documents in a manner independent of application software, hardware, and operating systems.[3] Each PDF file encapsulates a complete description of a fixed-layout flat document, including the text, fonts, graphics, and other information needed to display it.

pdf(Portable Document Format的简称,意为“便携式文档格式”),是由Adobe Systems用于与应用程序、操作系统、硬件无关的方式进行文件交换所发展出的文件格式。PDF文件以PostScript语言图象模型为基础,无论在哪种打印机上都可保证精确的颜色和准确的打印效果,即PDF会忠实地再现原稿的每一个字符、颜色以及图象。
rsc.io/pdf

github地址:
https://github.com/rsc/pdf

Star: 202

文档地址:
https://godoc.org/rsc.io/pdf

获取:

go get rsc.io/pdf

读取PDF文件,获取总页数

package main

import (
"fmt"

"rsc.io/pdf"
)

func main() {
file, err := pdf.Open("go.pdf")
if err != nil {
panic(err)
}
fmt.Println(file.NumPage())
}

读取某一页的内容

package main

import (
"fmt"

"rsc.io/pdf"
)

func main() {
file, err := pdf.Open("test.pdf")
if err != nil {
panic(err)
}
fmt.Println(file.Page(2).Content())
}

jung-kurt/gofpdf

注意:该库不支持中文!!!

github地址:
https://github.com/jung-kurt/gofpdf

Star: 733

文档地址:
https://godoc.org/github.com/jung-kurt/gofpdf

获取:

go get github.com/jung-kurt/gofpdf

生成PDF文档

package main

import (
"fmt"

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

func main() {
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "B", 26)
pdf.Cell(40, 10, "Hello PDF World")
err := pdf.OutputFileAndClose("write_pdf.pdf")
if err != nil {
fmt.Println(err)
}
}

生成加密的PDF

package main

import (
"fmt"

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

func main() {
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.SetProtection(gofpdf.CnProtectPrint, "123", "abc")
pdf.AddPage()
pdf.SetFont("Arial", "", 12)
pdf.Write(10, "You Must Enter the Password!!!")
err := pdf.OutputFileAndClose("write_pdf_with_password.pdf")
if err != nil {
fmt.Println(err)
}
}

PDF中插入图片

package main

import (
"fmt"

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

func main() {
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "", 11)
pdf.Image("test.png", 10, 10, 30, 0, false, "", 0, "")
pdf.Text(50, 20, "test.png")
pdf.Image("test.gif", 10, 40, 30, 0, false, "", 0, "")
pdf.Text(50, 50, "test.gif")
pdf.Image("test.jpg", 10, 130, 30, 0, false, "", 0, "")
pdf.Text(50, 140, "test.jpg")

err := pdf.OutputFileAndClose("write_pdf_with_image.pdf")
if err != nil {
fmt.Println(err)
}
}

PDF中增加链接、HTML

package main

import (
"fmt"

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

func main() {
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Helvetica", "", 20)
_, lineHt := pdf.GetFontSize()
pdf.Write(lineHt, "To find out what's new in this tutorial, click ")
pdf.SetFont("", "U", 0)
link := pdf.AddLink()
pdf.WriteLinkID(lineHt, "here", link)
pdf.SetFont("", "", 0)
// Second page: image link and basic HTML with link
pdf.AddPage()
pdf.SetLink(link, 0, -1)
pdf.Image("test.png", 10, 12, 30, 0, false, "", 0, "http://blog.csdn.net/wangshubo1989?viewmode=contents")
pdf.SetLeftMargin(45)
pdf.SetFontSize(14)
_, lineHt = pdf.GetFontSize()
htmlStr := `You can now easily print text mixing different styles: bold, ` +
`italic, underlined, or all at once!

` +
`
You can also center text.
` +
`Or align it to the right.` +
`You can also insert links on text, such as ` +
`http://blog.csdn.net/wangshubo1989?viewmode=contents, or on an image: click on the logo.`
html := pdf.HTMLBasicNew()
html.Write(lineHt, htmlStr)
err := pdf.OutputFileAndClose("write_html.pdf")
if err != nil {
fmt.Println(err)
}
}

signintech/gopdf

既然jung-kurt/gofpdf不支持中文,那么就介绍一个支持中文的signintech/gopdf。

github地址:
https://github.com/signintech/gopdf

Star: 422

获取:

go get -u github.com/signintech/gopdf

生成PDF文件

为了炫酷一点,自己从网上下载一个字体。

package main

import (
"log"

"github.com/signintech/gopdf"
)

func main() {

pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
pdf.AddPage()
err := pdf.AddTTFFont("wts11", "TTENuoJ_0.ttf")
if err != nil {
log.Print(err.Error())
return
}

err = pdf.SetFont("wts11", "", 14)
if err != nil {
log.Print(err.Error())
return
}
pdf.Cell(nil, "我闭目在经殿的香雾中, 蓦然听见你颂经中的真言;")
pdf.WritePdf("hello.pdf")

}


总数:1 页次:1/1 首页 尾页  
总数:1 页次:1/1 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1