【问题标题】:Create pdf from html in golang在golang中从html创建pdf
【发布时间】:2013-02-02 03:10:15
【问题描述】:

如何从 Google Go 中的 HTML 输入创建 PDF 文件?如果还不可能,是否有任何旨在解决此问题的倡议?

我正在寻找类似 TCPDF in php 的解决方案。

【问题讨论】:

标签: pdf pdf-generation go


【解决方案1】:

gopdf (https://github.com/signintech/gopdf) 怎么样。

您似乎正在寻找。

【讨论】:

  • 这些库都没有解决这个问题。用户正在寻找 HTML 到 PDF。这些只是 PDF 生成器。虽然它们可能很好,但它们都不能将 HTML 文档转换为 PDF。我需要在单独的线程中问同样的问题。
  • 以上两个依赖都不适用于<html> to .pdf
【解决方案2】:

安装

go get -u github.com/SebastiaanKlippert/go-wkhtmltopdf

go version go1.9.2 linux/amd64

代码

   import (
        "fmt"
        "strings"
        wkhtml "github.com/SebastiaanKlippert/go-wkhtmltopdf"
    )  

      func main(){
                 pdfg, err :=  wkhtml.NewPDFGenerator()
               if err != nil{
                  return
              }
              htmlStr := `<html><body><h1 style="color:red;">This is an html
 from pdf to test color<h1><img src="http://api.qrserver.com/v1/create-qr-
code/?data=HelloWorld" alt="img" height="42" width="42"></img></body></html>`

              pdfg.AddPage(wkhtml.NewPageReader(strings.NewReader(htmlStr)))


              // Create PDF document in internal buffer
              err = pdfg.Create()
              if err != nil {
                  log.Fatal(err)
              }

               //Your Pdf Name
               err = pdfg.WriteFile("./Your_pdfname.pdf")
              if err != nil {
                  log.Fatal(err)
              }

              fmt.Println("Done")
        }

以上代码适用于在 golang 中使用适当的背景图像和嵌入式 Css 样式标签将 html 转换为 pdf

Check repo

See Pull request Documentation Improved

【讨论】:

    【解决方案3】:

    还有这个包wkhtmltopdf-go,它使用了libwkhtmltox库。我不确定它有多稳定。

    【讨论】:

    • 僵尸进程不断占用我的 CPU 真是太可怕了。
    【解决方案4】:

    我认为我不了解您的要求。由于 HTML 是一种标记语言,它需要上下文来呈现(CSS 和屏幕大小)。我见过的现有实现通常在无头浏览器中打开页面并以这种方式创建 PDF。

    就个人而言,我只会使用现有的包并从 Go 中脱壳。 This one 看起来不错;甚至在this answer 中推荐。

    如果你真的决定在 Go 中实现这一切,请查看this WebKit wrapper。我不确定你会用什么来生成 PDF,但至少这是一个开始。

    【讨论】:

    • 我没有太多特殊要求。我需要创建 pdf 文件,但最好不是来自 go 代码,而是来自在灵活性和易于学习之间取得良好折衷的来源。在 php 中,有多个库可以将 HTML 文档转换为 pdf,因为 HTML 易于学习,而且非常灵活。我很好奇是否有人已经编写了这样的库。谢谢你的回答。
    【解决方案5】:

    我正在创建一个替代库以更简单的方式创建 PDF (https://github.com/johnfercher/maroto)。它使用 gofpdf 并有一个网格系统和一些组件,如 Bootstrap

    【讨论】:

      【解决方案6】:

      另一个选项是Athena。它有一个用 Go 编写的微服务,也可以用作 CLI。

      【讨论】:

        【解决方案7】:

        page.PrintToPDF() 功能很好用。

        这是一个使用 chromedp (go get -u github.com/chromedp/chromedp) 的示例:

        import (
            "context"
            "fmt"
            "io/ioutil"
            "log"
            "net/http"
            "os"
            "time"
        
            "github.com/chromedp/cdproto/emulation"
            "github.com/chromedp/cdproto/page"
            "github.com/chromedp/chromedp"
        )
        
        func main() {
                taskCtx, cancel := chromedp.NewContext(
                    context.Background(),
                    chromedp.WithLogf(log.Printf),
                )
                defer cancel()
                var pdfBuffer []byte
                if err := chromedp.Run(taskCtx, pdfGrabber("https://www.wikipedia.org", "body", &pdfBuffer)); err != nil {
                    log.Fatal(err)
                }
                if err := ioutil.WriteFile("coolsite.pdf", pdfBuffer, 0644); err != nil {
                    log.Fatal(err)
                }
        }
        
        func pdfGrabber(url string, sel string, res *[]byte) chromedp.Tasks {
        
            start := time.Now()
            return chromedp.Tasks{
                emulation.SetUserAgentOverride("WebScraper 1.0"),
                chromedp.Navigate(url),
                // wait for footer element is visible (ie, page is loaded)
                // chromedp.ScrollIntoView(`footer`),
                chromedp.WaitVisible(`body`, chromedp.ByQuery),
                // chromedp.Text(`h1`, &res, chromedp.NodeVisible, chromedp.ByQuery),
                chromedp.ActionFunc(func(ctx context.Context) error {
                    buf, _, err := page.PrintToPDF().WithPrintBackground(true).Do(ctx)
                    if err != nil {
                        return err
                    }
                    *res = buf
                    //fmt.Printf("h1 contains: '%s'\n", res)
                    fmt.Printf("\nTook: %f secs\n", time.Since(start).Seconds())
                    return nil
                }),
            }
        }
        

        以上内容将在 chrome 中无头加载 wikipedia.org 并等待正文显示,然后将其保存为 pdf。

        终端结果:

        $ go run main.go
        https://www.wikipedia.org
        Scraping url now...
        
        Took: 2.772797 secs
        

        【讨论】:

          【解决方案8】:

          另一个选项是 UniHTML(基于容器的 API),它与 UniPDF 互操作,这对于基于 HTML 模板创建 PDF 报告等很有用。

          它在容器中使用了无头chrome引擎,因此呈现完美,并具有所有HTML功能。与 UniPDF 的结合带来了额外的优势,例如自动生成目录、大纲等。以及添加密码保护、添加 PDF 表单、数字签名等功能。

          要为磁盘上的 HTML 模板创建 PDF,可以通过以下方式完成:

          package main
          
          import (
              "fmt"
              "os"
          
              "github.com/unidoc/unihtml"
              "github.com/unidoc/unipdf/v3/common/license"
              "github.com/unidoc/unipdf/v3/creator"
          )
          
          func main() {
              // Set the UniDoc license.
              if err := license.SetMeteredKey("my api key goes here"); err != nil {
                  fmt.Printf("Err: setting metered key failed: %v\n", err)
                  os.Exit(1)
              }
          
              // Establish connection with the UniHTML Server.
              if err := unihtml.Connect(":8080"); err != nil {
                  fmt.Printf("Err:  Connect failed: %v\n", err)
                  os.Exit(1)
              }
          
              // Get new PDF Creator.
              c := creator.New()
          
              // AddTOC enables Table of Contents generation.
              c.AddTOC = true
          
              chapter := c.NewChapter("Points")
          
              // Read the content of the sample.html file and load it to the conversion.
              htmlDocument, err := unihtml.NewDocument("sample.html")
              if err != nil {
                  fmt.Printf("Err: NewDocument failed: %v\n", err)
                  os.Exit(1)
              }
          
              // Draw the html document file in the context of the creator.
              if err = chapter.Add(htmlDocument); err != nil {
                  fmt.Printf("Err: Draw failed: %v\n", err)
                  os.Exit(1)
              }
          
              if err = c.Draw(chapter); err != nil {
                  fmt.Printf("Err: Draw failed: %v\n", err)
                  os.Exit(1)
              }
          
          
              // Write the result file to PDF.
              if err = c.WriteToFile("sample.pdf"); err != nil {
                  fmt.Printf("Err: %v\n", err)
                  os.Exit(1)
              }
          }
          

          我在 [此处] 写了一篇 UniHTML 的介绍文章,如果需要更多信息 (https://www.unidoc.io/post/html-for-pdf-reports-in-go) 可能会很有用。

          披露:我是 UniPDF 的原始开发者。

          【讨论】:

          • 您可能想警告人们您的解决方案需要支付至少 1500 美元。来源:unidoc.io/pricing
          猜你喜欢
          • 1970-01-01
          • 2012-05-22
          • 2017-01-22
          • 1970-01-01
          • 1970-01-01
          • 2017-03-25
          • 1970-01-01
          • 2017-05-28
          • 2012-07-10
          相关资源
          最近更新 更多