安装

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 := `

This is an html

from pdf to test color

img`

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

}

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

Logo

更多推荐