golang讀取pdf
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
- 1
讀取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())
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
讀取某一頁的內容
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())
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
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
- 1
生成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)
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
生成加密的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)
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
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)
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
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: <b>bold</b>, ` +
`<i>italic</i>, <u>underlined</u>, or <b><i><u>all at once</u></i></b>!<br><br>` +
`<center>You can also center text.</center>` +
`<right>Or align it to the right.</right>` +
`You can also insert links on text, such as ` +
`<a href="http://www.fpdf.org">http://blog.csdn.net/wangshubo1989?viewmode=contents</a>, 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)
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
signintech/gopdf
既然jung-kurt/gofpdf不支援中文,那麼就介紹一個支援中文的signintech/gopdf。
github地址:
https://github.com/signintech/gopdf
Star: 422
獲取:
go get -u github.com/signintech/gopdf
- 1
生成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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
相關文章
- python 讀取PDF表格Python
- Java 讀取PDF中的表格Java
- RAG學習--pdf讀取與切割
- Java 建立、填充、讀取PDF表單域Java
- Java 讀取PDF中的文字和圖片Java
- Python如何讀取pdf中的圖片Python
- Golang專案中讀取配置檔案Golang
- 用R讀取PDF並進行資料探勘
- Golang 超大檔案讀取的兩個方案Golang
- golang讀取java或者go的webservice服務GolangJavaWeb
- golang 讀取切分儲存byte流檔案Golang
- Java PDF書籤——新增、編輯、刪除、讀取書籤Java
- Golang 快速讀取處理大日誌檔案工具Golang
- Blazor Pdf Reader PDF閱讀器 元件 更新Blazor元件
- PDF Reader Pro for Mac專業PDF閱讀器Mac
- pdf編輯閱讀器PDF Reader Pro for MacMac
- 批量擷取pdf檔案
- 利用html5 file api讀取本地檔案(如圖片、PDF等)HTMLAPI
- Golang 讀、寫檔案Golang
- PDF Reader Pro Lite for Mac專業PDF閱讀器Mac
- 「pdf檔案閱讀器」PDF Reader Pro 2.8.19.1
- web版pdf線上閱讀器Web
- PDF Reader Pro for mac(pdf閱讀器)2.9.9.0啟用版Mac
- PDF Reader Pro for mac(pdf閱讀器) 3.0.1.0啟用版Mac
- PDF Reader Pro Mac強大的專業PDF閱讀器Mac
- Blazor元件自做十二 : Blazor Pdf Reader PDF閱讀器 元件Blazor元件
- 獲取和設定pdf目錄
- lucene開發中有關讀取pdf,html,word,rtf,txt,powerpoint,excel等文件的操作HTMLExcel
- Golang 獲取 goroutine id 完全指南Golang
- golang獲取程式執行路徑Golang
- golang 介面按需獲取資源Golang
- Golang對檔案讀寫操作Golang
- PDF閱讀和編輯工具PDF Reader Pro for mac啟用版Mac
- 必備的PDF閱讀器:PDF Reader Pro Mac中文版Mac
- 福昕PDF閱讀器 Foxit PDF Reader 中文綠色版
- cookie讀取Cookie
- XSS 從 PDF 中竊取資料
- VB讀取文字檔案的例子:逐行讀取