drydaenth257216154 2016-02-10 06:25
浏览 52
已采纳

golang用西里尔文创建pdf

I need to create pdf file in go with cyrillic symbols. I've started with https://github.com/jung-kurt/gofpdf but it needs 1251 symbols to produce correct cyrillic. I've tried

package main

import (
    "github.com/jung-kurt/gofpdf"
    "fmt"
    "os"
)

func main() {
    pwd, err := os.Getwd()
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    pdf := gofpdf.New("P", "mm", "A4", "")
    pdf.AddFont("Helvetica", "", pwd + "/font/helvetica_1251.json")
    pdf.AddPage()
    pdf.SetFont("Helvetica", "", 16)
    tr := pdf.UnicodeTranslatorFromDescriptor("cp1251")
    pdf.Cell(15, 50, tr("русский текст"))
    pdf.OutputFileAndClose("test.pdf")
}

but it produce only dots instead of text.

Then I've tried to use https://github.com/golang/freetype to create image with text and then insert it to pdf. So I've tried

package main

import (
    "github.com/jung-kurt/gofpdf"
    "github.com/golang/freetype"
    "image"
    "fmt"
    "os"
    "bytes"
    "image/jpeg"
    "io/ioutil"
    "image/draw"
)

func main() {
    pwd, err := os.Getwd()
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    dataFont, err := ioutil.ReadFile(pwd + "/font/luxisr.ttf")
    if err != nil {
        fmt.Printf("%v",err)
    }
    f, err := freetype.ParseFont(dataFont)
    if err != nil {
        fmt.Printf("%v",err)
    }
    dst := image.NewRGBA(image.Rect(0, 0, 800, 600))
    draw.Draw(dst, dst.Bounds(), image.White, image.ZP, draw.Src)
    c := freetype.NewContext()
    c.SetDst(dst)
    c.SetClip(dst.Bounds())
    c.SetSrc(image.Black)
    c.SetFont(f)
    c.DrawString("русский текст", freetype.Pt(0, 16))
    pdf := gofpdf.New("P", "mm", "A4", "")
    pdf.AddPage()
    buf := new(bytes.Buffer)
    err = jpeg.Encode(buf, dst, nil)

    if err != nil {
        fmt.Printf("%v",err)
    }

    reader := bytes.NewReader(buf.Bytes())
    textName := "text1"
    pdf.RegisterImageReader(textName, "jpg", reader)
    pdf.Image(textName, 15, 15, 0, 0, false, "jpg", 0, "")
    pdf.OutputFileAndClose("test.pdf")
}

but as a result I receive squares instead of text because it seems that freetype needs unicode symbols for text.

Is it possible to convert strings which are generally in utf-8 to unicode? How can I create pdf or image with cyrillic text?

Thank you.

  • 写回答

1条回答 默认 最新

  • doukuang8166 2016-02-10 08:39
    关注

    First, you are ignoring an error in the final line. pdf.OutputFileAndClose returns an error, so you should check it:

    err := pdf.OutputFileAndClose("test.pdf")
    if err != nil {
        log.Fatal(err)
    }
    

    Other than that, your first example works for me. The generated output looks like this:

    PDF Output

    Here is the code I used, you'll see it's very similar to yours:

    package main
    
    import (
        "log"
    
        "github.com/jung-kurt/gofpdf"
    )
    
    func main() {
        pdf := gofpdf.New("P", "mm", "A4", "")
        pdf.AddFont("Helvetica", "", "helvetica_1251.json")
        pdf.AddPage()
        pdf.SetFont("Helvetica", "", 16)
        tr := pdf.UnicodeTranslatorFromDescriptor("cp1251")
        pdf.Cell(15, 50, tr("русский текст"))
        err := pdf.OutputFileAndClose("test.pdf")
        if err != nil {
            log.Println(err)
        }
    }
    

    With the above code, the important thing is to make sure helvetica_1251.z, helvetica_1251.json and cp1251.map (from $GOPATH/src/github.com/jung-kurt/gofpdf/font, or generated by the makefont tool) are all in the current directory. If you can confirm that this works for you, you can proceed to move them into the fonts directory and changing the code accordingly. My best guess is that you may be silently ignoring an error warning you about one of these files.

    PS I'm running Mac OS X. If you are on another system, make sure you have a version of Helvetica with cyrillic character support installed.

    Update

    For others facing this problem in the future, I wanted to add the final solution here. From the comments below:

    Thanks to jung-kurt I found solution. You can avoid this bug on Windows by adding pdf.SetCompression(true) – Timur Shahmuratov

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化