diff --git a/go.mod b/go.mod index c4436c68..3f2b5328 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module fusenapi go 1.20 require ( + github.com/SebastiaanKlippert/go-wkhtmltopdf v1.9.0 github.com/golang-jwt/jwt v3.2.2+incompatible - github.com/google/uuid v1.3.0 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/zeromicro/go-zero v1.5.2 @@ -15,6 +15,7 @@ require ( require ( github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210521184019-c5ad59b459ec // indirect + github.com/google/uuid v1.3.0 // indirect github.com/logrusorgru/aurora v2.0.3+incompatible // indirect ) diff --git a/go.sum b/go.sum index 417ae23c..8419a3fe 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/SebastiaanKlippert/go-wkhtmltopdf v1.9.0 h1:DNrExYwvyyI404SxdUCCANAj9TwnGjRfa3cYFMNY1AU= +github.com/SebastiaanKlippert/go-wkhtmltopdf v1.9.0/go.mod h1:SQq4xfIdvf6WYKSDxAJc+xOJdolt+/bc1jnQKMtPMvQ= github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk= github.com/alicebob/miniredis/v2 v2.30.2 h1:lc1UAUT9ZA7h4srlfBmBt2aorm5Yftk9nBjxz7EyY9I= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= diff --git a/utils/pdf/html_to_pdf.go b/utils/pdf/html_to_pdf.go new file mode 100644 index 00000000..0469f936 --- /dev/null +++ b/utils/pdf/html_to_pdf.go @@ -0,0 +1,35 @@ +package pdf + +import ( + "github.com/SebastiaanKlippert/go-wkhtmltopdf" + "strings" +) + +// html转 Pdf dataType = 1 为网页url dataType = 2为网页内容 outFile为空则不保存 +func HtmlToPdf(content string, dataType int, outFile string) ([]byte, error) { + pdfg, err := wkhtmltopdf.NewPDFGenerator() + if err != nil { + return nil, err + } + switch dataType { + case 1: //网页地址 + pdfg.AddPage(wkhtmltopdf.NewPage(content)) + case 2: //网页内容 + pdfg.AddPage(wkhtmltopdf.NewPageReader(strings.NewReader(content))) + } + //模式 + pdfg.Orientation.Set(wkhtmltopdf.OrientationPortrait) + //pdf尺寸默认A4纸 + pdfg.PageSize.Set(wkhtmltopdf.PageSizeA4) + // Create PDF document in memory + if err = pdfg.Create(); err != nil { + return nil, err + } + // Write PDF to file + if outFile != "" { + if err = pdfg.WriteFile(outFile); err != nil { + return nil, err + } + } + return pdfg.Bytes(), nil +}