Go combat--golang in PDF (Rsc.io/pdf, jung-kurt/gofpdf, Signintech/gopdf)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Life goes on and on go Go go!!!

Yesterday describes how Excel is manipulated in Golang:
Go combat –golang Excel (Tealeg/xlsx, 360entsecgroup-skylar/excelize)


So let's share with you today how PDFs work in Golang.


PDF Introduction

The Portable Document format (PDF) is a file format used to present documents in a manner independent of application SOFTW is, 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 (abbreviated as "Portable Document Format"), which is used by Adobe Systems for file exchange with application, operating system, and hardware portable, is a file format that is displayed on the document. The PDF file is based on a PostScript language image model that guarantees accurate color and accurate printing on any printer, that is, the PDF faithfully reproduces every character, color, and image of the original.


Rsc.io/pdf

GitHub Address:
Https://github.com/rsc/pdf

star:202

Document Address:
Https://godoc.org/rsc.io/pdf


Get:

get rsc.io/pdf


Read PDF file to get total pages

package main

import (
    "fmt"

    "rsc.io/pdf"
)

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


Read the contents of a page

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

Note: The library does not support Chinese!!!


GitHub Address:
Https://github.com/jung-kurt/gofpdf

star:733

Document Address:
Https://godoc.org/github.com/jung-kurt/gofpdf


Get:

go get github.com/jung-kurt/gofpdf


Generate PDF documents

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)
    }
}


Generate an encrypted 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)
    }
}

Insert a picture in 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)
    }
}

Add links, HTML in pdf

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)
    }
}

Signintech/gopdf

Since Jung-kurt/gofpdf does not support Chinese, it introduces a signintech/gopdf that supports Chinese.

GitHub Address:
Https://github.com/signintech/gopdf

star:422

Get:

go get -u github.com/signintech/gopdf


Generate PDF files

To be cool, download a font yourself from the Web.

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, "I closed my eyes in the fragrant mist of the temple, and suddenly heard the truth in your chanting;")
     pdf.WritePdf("hello.pdf")

}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.