实现Linux下导出pdf

This commit is contained in:
Minho 2017-05-09 10:04:56 +08:00
parent 0934654231
commit f7fe35e43a
3 changed files with 95 additions and 18 deletions

View File

@ -566,9 +566,9 @@ func (c *DocumentController) Export() {
c.TplName = "document/export.tpl"
identify := c.Ctx.Input.Param(":key")
// token := c.GetString("token")
output := c.GetString("output")
//id, _ := c.GetInt(":id")
token := c.GetString("token")
if identify == "" {
@ -629,7 +629,7 @@ func (c *DocumentController) Export() {
c.StopRun()
}
//递归生成文档序列数组.
func RecursiveFun(parent_id int,prefix,dpath string,c *DocumentController,book *models.BookResult,docs []*models.Document,paths *list.List) {
for _, item := range docs {
if item.ParentId == parent_id {

75
utils/pdf_linux.go Normal file
View File

@ -0,0 +1,75 @@
package utils
import (
"os/exec"
"bufio"
"io"
"io/ioutil"
"errors"
"github.com/astaxie/beego"
)
// 使用 wkhtmltopdf 是实现 html 转 pdf.
// 中文说明http://www.jianshu.com/p/4d65857ffe5e#
func ConverterHtmlToPdf(uri []string,path string) (error) {
exe := beego.AppConfig.String("wkhtmltopdf")
if exe == "" {
return errors.New("wkhtmltopdf not exist.")
}
params := []string{"/C",exe,"--margin-bottom","25"}
params = append(params,uri...)
params = append(params,path)
cmd := exec.Command("/bin/bash",params...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return errors.New("StdoutPipe: " + err.Error())
}
stderr, err := cmd.StderrPipe()
if err != nil {
return errors.New("StderrPipe: " + err.Error())
}
if err := cmd.Start(); err != nil {
return errors.New("Start: "+ err.Error())
}
reader := bufio.NewReader(stdout)
//实时循环读取输出流中的一行内容
for {
line ,err2 := reader.ReadString('\n')
if err2 != nil || io.EOF == err2 {
break
}
beego.Info(line)
}
bytesErr, err := ioutil.ReadAll(stderr)
if err == nil {
beego.Info(string(bytesErr))
}else{
beego.Error("Error: Stderr => " + err.Error())
return err
}
if err := cmd.Wait(); err != nil {
beego.Error("Error: ", err.Error())
return err
}
return nil
}

View File

@ -2,20 +2,23 @@ package utils
import (
"os/exec"
"fmt"
"bufio"
"github.com/axgle/mahonia"
"io"
"io/ioutil"
"errors"
"github.com/axgle/mahonia"
"github.com/astaxie/beego"
)
// 使用 wkhtmltopdf 是实现 html 转 pdf.
// 中文说明http://www.jianshu.com/p/4d65857ffe5e#
func ConverterHtmlToPdf(uri []string,path string) {
func ConverterHtmlToPdf(uri []string,path string) (error) {
exe := beego.AppConfig.String("wkhtmltopdf")
if exe == "" {
panic("wkhtmltopdf not exist.")
return errors.New("wkhtmltopdf not exist.")
}
params := []string{"/C",exe,"--margin-bottom","25"}
@ -28,18 +31,17 @@ func ConverterHtmlToPdf(uri []string,path string) {
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println("StdoutPipe: " + err.Error())
return
return errors.New("StdoutPipe: " + err.Error())
}
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Println("StderrPipe: ", err.Error())
return
return errors.New("StderrPipe: " + err.Error())
}
if err := cmd.Start(); err != nil {
fmt.Println("Start: ", err.Error())
return
return errors.New("Start: "+ err.Error())
}
reader := bufio.NewReader(stdout)
@ -53,25 +55,25 @@ func ConverterHtmlToPdf(uri []string,path string) {
break
}
beego.Error(enc.ConvertString(line))
beego.Info(enc.ConvertString(line))
}
bytesErr, err := ioutil.ReadAll(stderr)
if err == nil {
beego.Error(enc.ConvertString(string(bytesErr)))
beego.Info(enc.ConvertString(string(bytesErr)))
}else{
beego.Error("Error: Stderr => " + err.Error())
return err
}
if err := cmd.Wait(); err != nil {
beego.Error("Error: ", err.Error())
return
return err
}
return
return nil
}