Create pdf from html in golang

47,728

Solution 1

what about gopdf (https://github.com/signintech/gopdf).

It seems like you are looking for.

Solution 2

Installation

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

go version go1.9.2 linux/amd64

code

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

The Above code Works for Converting html to pdf in golang with proper background image and Embedded Css Style Tags

Check repo

See Pull request Documentation Improved

Solution 3

There is also this package wkhtmltopdf-go, which uses the libwkhtmltox library. I am not sure how stable it is though.

Solution 4

I don't think I understand your requirements. Since HTML is a markup language, it needs context to render (CSS and a screen size). Existing implementations I've seen generally open the page in a headless browser and create a PDF that way.

Personally, I would just use an existing package and shell out from Go. This one looks good; it's even recommended in this answer.

If you're really determined to implement it all in Go, check out this WebKit wrapper. I'm not sure what you'd use for generating PDFs, but but at least it's a start.

Solution 5

The function page.PrintToPDF() works great.

Here is an example using it with 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
        }),
    }
}

The above will load wikipedia.org in chrome headless and wait for body to show up and then save it as pdf.

results in terminal:

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

Took: 2.772797 secs
Share:
47,728
mimrock
Author by

mimrock

Updated on August 16, 2021

Comments

  • mimrock
    mimrock almost 3 years

    How to create PDF files from an HTML input in Google Go? If it is not possible yet, are there any initations that aims to solve this problem?

    I'm looking for a solution like TCPDF in php.

  • mimrock
    mimrock over 11 years
    I do not have too many special requirements. I need to create pdf files, but preferably not from go code, but from a source that is a good compromise between flexibility and easy learning. In php, there are multiple libraries for converting HTML documents to pdf, because HTML is easy to learn, and pretty flexible. I was curious if someone has already written a library like that. Thank you for your answer.
  • George Thomas
    George Thomas about 8 years
    It is pretty horrible kept hogging my CPU with zombie processes.
  • taystack
    taystack over 6 years
    Neither of these libraries address the question. The user is looking for HTML to PDF. These are just PDF generators. While they may be good ones, neither converts an HTML document into a PDF. I need to ask this same question in a separate thread.
  • muthukumar selvaraj
    muthukumar selvaraj over 6 years
    The above both Dependencies won't work for <html> to .pdf
  • ewen-lbh
    ewen-lbh about 3 years
    You might want to warn people that your solution requires paying at least $1500. Source: unidoc.io/pricing
  • Hamed Lohi
    Hamed Lohi about 2 years
    This answer seems to be correct : stackoverflow.com/a/48568415/8730051
  • Igor
    Igor about 2 years
    go-wkhtmltopdf depends on wkhtmltopdf binary. It have to be installed to system before. And wkhtmltopdf binary depends on about 50 or 60 packages from xserver. It is not suitable for backend solution at all.