将HTML转换为PDF并下载PDF文件

时间:2018-07-11 06:06:53

标签: go

我需要下载pdf格式的html表以生成报告。有什么方法可以将html表转换为pdf并下载,而不是在浏览器中查看吗?

2 个答案:

答案 0 :(得分:0)

尝试使用此库,在将html转换为pdf之前,我已经使用过它,并且在我们大多数情况下都可以正常使用https://github.com/SebastiaanKlippert/go-wkhtmltopdf

答案 1 :(得分:0)

就我而言,使用PDFShift之类的在线API可以正常工作:

package main

import (
    "fmt"
    "strings"
    "net/http"
    "io/ioutil"
)

func main() {

    url := "https://api.pdfshift.io/v2/convert"
    var jsonStr = []byte(`{"source":"your_html_content_here"}`)

    req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    req.SetBasicAuth("Your_API_KEY", "")
    res, _ := http.DefaultClient.Do(req)

    defer res.Body.Close()
    body, _ := ioutil.ReadAll(res.Body)

    fmt.Println(res)
    fmt.Println(string(body))
}
相关问题