Generate PDF in Golang

Asked

Viewed 114 times

-1

Good morning everyone, I am new to the Golang language and I am having difficulties in a text printing file in PDF generation that I was asked to study. The difficulty is in creating the variable "err" in which I can’t develop, I was able to notice that the error is due to the source that the file is trying to use and my machine does not have the file of this font. Since it’s a Github file, I didn’t make it, I got it ready to analyze the structure, I’ll make the link available. Thanks in advance, and if you’re confused the question I’m sorry, because it’s my first time here. https://github.com/signintech/gopdf

package main    
import (
    "log"    
    "github.com/signintech/gopdf"
)

func main() {    
    pdf := gopdf.GoPdf{}
    pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
    pdf.AddPage()
    //err := pdf.AddTTFFont("wts11", "../ttf/wts11.ttf")
    err := pdf.
    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 answer

0

It is possible to compile this example, follow the steps I have done to successfully run:

I created a project with the command:

$ go mod init example.com/x

Create a project with the following folder structure:

.
├── _main.go
├── _ttf
    └── wts11.ttf

The archive wts11.ttf can be downloaded from the repository containing the lib gopdf. Ref. Open in a browser and download the file (approx. 10.1 MB).

After downloading put the downloaded file in the hierarchy of folders described above.

It will now be possible to run the program:

main go.

package main

import (
    "log"

    "github.com/signintech/gopdf"
)

func main() {
    pdf := gopdf.GoPdf{}
    pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
    pdf.AddPage()
    err := pdf.AddTTFFont("wts11", "ttf/wts11.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")
}

Assuming it is at the root of the project directory and followed the above instructions:

$ go run main.go

After successful execution there should be a file hello.pdf at the root of the project directory.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.