mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-11-24 08:47:04 +08:00
实现导出pdf
This commit is contained in:
@@ -287,16 +287,16 @@ func getFooter(po *PageOptions, totalpages int) string {
|
||||
*/
|
||||
func setDefault(po *PageOptions, totalpages int) *PageOptions {
|
||||
if len(po.FirstPageText) <= 0 {
|
||||
po.FirstPageText = "首页"
|
||||
po.FirstPageText = "«"
|
||||
}
|
||||
if len(po.LastPageText) <= 0 {
|
||||
po.LastPageText = "尾页"
|
||||
po.LastPageText = "»"
|
||||
}
|
||||
if len(po.PrePageText) <= 0 {
|
||||
po.PrePageText = "上一页"
|
||||
po.PrePageText = "‹"
|
||||
}
|
||||
if len(po.NextPageText) <= 0 {
|
||||
po.NextPageText = "下一页"
|
||||
po.NextPageText = "›"
|
||||
}
|
||||
if po.CurrentPage >= totalpages {
|
||||
po.CurrentPage = totalpages
|
||||
|
||||
22
utils/pdf.go
22
utils/pdf.go
@@ -1,22 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/jung-kurt/gofpdf"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
func ConverterPdf(output string,htmlList map[string]string) error {
|
||||
|
||||
pdf := gofpdf.New("P", "mm", "A4", "./static/pdf-fonts/msyh.ttf")
|
||||
|
||||
pdf.AddPage()
|
||||
|
||||
pdf.SetFont("微软雅黑","B",14)
|
||||
pdf.Cell(40, 10, "Hello, world")
|
||||
err := pdf.OutputFileAndClose("hello.pdf")
|
||||
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
77
utils/pdf_windows.go
Normal file
77
utils/pdf_windows.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"fmt"
|
||||
"bufio"
|
||||
"github.com/axgle/mahonia"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
// 中文说明:http://www.jianshu.com/p/4d65857ffe5e#
|
||||
func ConverterHtmlToPdf(uri []string,path string) {
|
||||
|
||||
exe := beego.AppConfig.String("wkhtmltopdf")
|
||||
|
||||
if exe == "" {
|
||||
panic("wkhtmltopdf not exist.")
|
||||
}
|
||||
|
||||
params := []string{"/C",exe,"--margin-bottom","25"}
|
||||
|
||||
params = append(params,uri...)
|
||||
params = append(params,path)
|
||||
|
||||
cmd := exec.Command("cmd",params...)
|
||||
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("StdoutPipe: " + err.Error())
|
||||
return
|
||||
}
|
||||
stderr, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
fmt.Println("StderrPipe: ", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
fmt.Println("Start: ", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(stdout)
|
||||
enc := mahonia.NewDecoder("gbk")
|
||||
|
||||
//实时循环读取输出流中的一行内容
|
||||
for {
|
||||
line ,err2 := reader.ReadString('\n')
|
||||
|
||||
if err2 != nil || io.EOF == err2 {
|
||||
break
|
||||
}
|
||||
|
||||
beego.Error(enc.ConvertString(line))
|
||||
}
|
||||
|
||||
bytesErr, err := ioutil.ReadAll(stderr)
|
||||
|
||||
if err == nil {
|
||||
beego.Error(enc.ConvertString(string(bytesErr)))
|
||||
}else{
|
||||
beego.Error("Error: Stderr => " + err.Error())
|
||||
}
|
||||
|
||||
if err := cmd.Wait(); err != nil {
|
||||
|
||||
beego.Error("Error: ", err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user