zoukankan      html  css  js  c++  java
  • johnfercher/maroto 不错的pdf golang pdf 生成工具包

    johnfercher/maroto 借鉴了bootstrap 的网格模式,使用了gofpdf 生成pdf,是一个很不错的golang pdf 工具
    有一个问题是不支持中文(因为配置写的的原因)看到网上有一个中国人fork添加了AddUTF8Font 支持,这样
    中文就可以显示了,以下是一个参考的使用

    参考特性

    核心代码

     
    package main
    import (
        "fmt"
        "os"
        "time"
        "github.com/Vale-sail/maroto/pkg/consts"
        "github.com/Vale-sail/maroto/pkg/pdf"
        "github.com/Vale-sail/maroto/pkg/props"
    )
    func main() {
        begin := time.Now()
        m := pdf.NewMarotoCustomSize(consts.Landscape, "C6", "mm", 114.0, 162.0)
        m.SetPageMargins(5, 5, 5)
        m.AddUTF8Font("NotoSansSC", "", "./font/NotoSansSC-Regular.ttf")
        m.AddUTF8Font("NotoSansSC", "I", "./font/NotoSansSC-Regular.ttf")
        m.AddUTF8Font("NotoSansSC", "B", "./font/NotoSansSC-Regular.ttf")
        m.AddUTF8Font("NotoSansSC", "BI", "./font/NotoSansSC-Regular.ttf")
        // m.SetBorder(true)
        m.Row(40, func() {
            m.Col(4, func() {
                _ = m.FileImage("biplane.jpg", props.Rect{
                    Center:  true,
                    Percent: 50,
                })
            })
            m.Col(4, func() {
                m.Text("Gopher International Shipping, Inc.", props.Text{
                    Top:         12,
                    Size:        12,
                    Extrapolate: true,
                })
            })
            m.ColSpace(4)
        })
        m.Line(10)
        m.Row(30, func() {
            m.Col(12, func() {
                m.Text("João Sant'Ana 100 Main Street", props.Text{
                    Size:  10,
                    Align: consts.Right,
                })
                m.Text("荣锋亮 TN 39021", props.Text{
                    Size:   10,
                    Align:  consts.Right,
                    Family: "NotoSansSC",
                    Top:    10,
                })
                m.Text("United States (USA)", props.Text{
                    Size:  10,
                    Align: consts.Right,
                    Top:   20,
                })
            })
        })
        m.Row(30, func() {
            m.Col(12, func() {
                m.QrCode("https://cnblogs.com/rongfengliang")
            })
        })
        err := m.OutputFileAndClose("customsize.pdf")
        if err != nil {
            fmt.Println("Could not save PDF:", err)
            os.Exit(1)
        }
        end := time.Now()
        fmt.Println(end.Sub(begin))
    }

    说明

    实际上同时也应该保留AddUTF8FontFromBytes,同时也应该添加中文编码的常量,可以方便软件的分发以及使用(后边fork修改一个版本)

    参考资料

    https://github.com/johnfercher/maroto
    https://github.com/Vale-sail/maroto

  • 相关阅读:
    WIn7 磁盘分区工具试用记录
    DirectShow 开发环境搭建(整理)
    WinCE 在连续创建约 1000 个文件后,再创建文件失败。这是为什么???
    在命令行处理 console 应用执行的返回值
    WinCE 的发展史及相关基础知识
    DirectShow Filter 基础与简单的示例程序
    使用 VS2005 编译 directshow sample 时链接错误
    车载系统之 Windows CE 应用软件框架设计
    兰州烧饼
    对决
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/14075971.html
Copyright © 2011-2022 走看看