diff --git a/conf/app.conf.example b/conf/app.conf.example
index c8a11eec..1e81563c 100644
--- a/conf/app.conf.example
+++ b/conf/app.conf.example
@@ -60,4 +60,8 @@ smtp_port=25
#发送邮件的显示名称
form_user_name=admin@iminho.me
#邮件有效期30分钟
-mail_expired=30
\ No newline at end of file
+mail_expired=30
+
+
+###############配置PDF生成工具地址###################
+wkhtmltopdf=D:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe
\ No newline at end of file
diff --git a/controllers/document.go b/controllers/document.go
index 0d15681f..18585727 100644
--- a/controllers/document.go
+++ b/controllers/document.go
@@ -10,11 +10,13 @@ import (
"path/filepath"
"encoding/json"
"html/template"
+ "container/list"
"github.com/lifei6671/godoc/models"
"github.com/lifei6671/godoc/conf"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
+ "github.com/lifei6671/godoc/utils"
)
//DocumentController struct.
@@ -558,10 +560,109 @@ func (c *DocumentController) Content() {
c.JsonResult(0,"ok",doc)
}
-
-
-
-
+//导出文件
+func (c *DocumentController) Export() {
+ c.Prepare()
+ 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 == "" {
+ c.Abort("404")
+ }
+ //如果没有开启你们访问则跳转到登录
+ if !c.EnableAnonymous && c.Member == nil {
+ c.Redirect(beego.URLFor("AccountController.Login"),302)
+ return
+ }
+ book := isReadable(identify,token,c)
+
+ if book.PrivatelyOwned == 1 {
+
+ }
+
+ docs, err := models.NewDocument().FindListByBookId(book.BookId)
+
+ if err != nil {
+ beego.Error(err)
+ c.Abort("500")
+ }
+
+ if output == "pdf" {
+ dpath := "cache/" + book.Identify
+
+ os.MkdirAll(dpath, 0766)
+
+ pathList := list.New()
+
+ RecursiveFun(0, "", dpath, c, book, docs, pathList)
+
+ defer os.RemoveAll(dpath)
+
+ os.MkdirAll("./cache", 0766)
+ pdfpath := "cache/" + identify + ".pdf"
+
+ paths := make([]string, len(docs))
+ index := 0
+ for e := pathList.Front(); e != nil; e = e.Next() {
+ paths[index] = e.Value.(string)
+ index ++
+ }
+
+ beego.Info(paths)
+
+ utils.ConverterHtmlToPdf(paths, pdfpath)
+
+
+
+ c.Ctx.Output.Download(pdfpath, identify + ".pdf")
+
+ defer os.Remove(pdfpath)
+
+ c.StopRun()
+ }
+
+ 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 {
+ name := prefix + strconv.Itoa(item.ParentId) + strconv.Itoa(item.OrderSort) + strconv.Itoa(item.DocumentId)
+ fpath := dpath + "/" + name + ".html"
+ paths.PushBack(fpath)
+
+
+ f, err := os.OpenFile(fpath, os.O_CREATE, 0666)
+
+ if err != nil {
+ beego.Error(err)
+ c.Abort("500")
+ }
+
+ html, err := c.ExecuteViewPathTemplate("document/export.tpl", map[string]interface{}{"Model" : book, "Lists":item,"BaseUrl" : c.BaseUrl()})
+ if err != nil {
+ f.Close()
+ beego.Error(err)
+ c.Abort("500")
+ }
+ f.WriteString(html)
+ f.Close()
+
+ for _, sub := range docs {
+ if sub.ParentId == item.DocumentId {
+ RecursiveFun(item.DocumentId,name,dpath,c,book,docs,paths)
+ break;
+ }
+ }
+ }
+ }
+}
diff --git a/main.go b/main.go
index b56f0546..aa4ef1e2 100644
--- a/main.go
+++ b/main.go
@@ -1,18 +1,19 @@
package main
import (
+ "fmt"
+ "os"
+
_ "github.com/go-sql-driver/mysql"
_ "github.com/lifei6671/godoc/routers"
_ "github.com/astaxie/beego/session/redis"
_ "github.com/astaxie/beego/session/memcache"
_ "github.com/astaxie/beego/session/mysql"
+
"github.com/astaxie/beego"
"github.com/lifei6671/godoc/commands"
- "fmt"
- "os"
"github.com/lifei6671/godoc/controllers"
"github.com/lifei6671/godoc/conf"
- "github.com/lifei6671/godoc/utils"
)
var (
@@ -37,7 +38,6 @@ func main() {
beego.ErrorController(&controllers.ErrorController{})
- utils.ConverterPdf("a.pdf",nil)
beego.Run()
}
diff --git a/models/document.go b/models/document.go
index 78ca72df..392f9f37 100644
--- a/models/document.go
+++ b/models/document.go
@@ -128,7 +128,13 @@ func (m *Document) ReleaseContent(book_id int) {
}
-
+func (m *Document) FindListByBookId(book_id int) (docs []*Document,err error) {
+ o := orm.NewOrm()
+
+ _,err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id",book_id).All(&docs)
+
+ return
+}
diff --git a/models/document_tree.go b/models/document_tree.go
index c3ef8e1a..dc8c88ec 100644
--- a/models/document_tree.go
+++ b/models/document_tree.go
@@ -100,19 +100,7 @@ func getDocumentTree(array []*DocumentTree,parent_id int,selected_id int,selecte
pid = p
}
if pid == parent_id {
- /**
- $selected = $item['doc_id'] == $selected_id ? ' class="jstree-clicked"' : '';
- $selected_li = $item['doc_id'] == $selected_parent_id ? ' class="jstree-open"' : '';
- $menu .= '
' . $item['doc_name'] .'';
-
- $key = array_search($item['doc_id'], array_column($array, 'parent_id'));
-
- if ($key !== false) {
- self::createTree($item['doc_id'], $array,$selected_id,$selected_parent_id);
- }
- $menu .= '';
- */
selected := ""
if item.DocumentId == selected_id {
selected = ` class="jstree-clicked"`
diff --git a/routers/router.go b/routers/router.go
index 0ed0fab0..0d2311f8 100644
--- a/routers/router.go
+++ b/routers/router.go
@@ -58,6 +58,7 @@ func init() {
beego.Router("/docs/:key", &controllers.DocumentController{},"*:Index")
beego.Router("/docs/:key/:id", &controllers.DocumentController{},"*:Read")
+ beego.Router("/export/:key", &controllers.DocumentController{},"*:Export")
beego.Router("/attach_files/:key/:attach_id",&controllers.DocumentController{},"get:DownloadAttachment")
diff --git a/static/pdf-fonts/msyh.ttc b/static/pdf-fonts/msyh.ttc
deleted file mode 100644
index 796cc2d9..00000000
Binary files a/static/pdf-fonts/msyh.ttc and /dev/null differ
diff --git a/static/pdf-fonts/msyh.ttf b/static/pdf-fonts/msyh.ttf
deleted file mode 100644
index aa23ae1f..00000000
Binary files a/static/pdf-fonts/msyh.ttf and /dev/null differ
diff --git a/static/pdf-fonts/msyhbd.ttc b/static/pdf-fonts/msyhbd.ttc
deleted file mode 100644
index 5e2f4dd9..00000000
Binary files a/static/pdf-fonts/msyhbd.ttc and /dev/null differ
diff --git a/static/pdf-fonts/msyhbold.ttf b/static/pdf-fonts/msyhbold.ttf
deleted file mode 100644
index d66a9709..00000000
Binary files a/static/pdf-fonts/msyhbold.ttf and /dev/null differ
diff --git a/static/pdf-fonts/msyhl.ttc b/static/pdf-fonts/msyhl.ttc
deleted file mode 100644
index 062adb96..00000000
Binary files a/static/pdf-fonts/msyhl.ttc and /dev/null differ
diff --git a/utils/pager.go b/utils/pager.go
index 8dafdcd4..1dcf7349 100644
--- a/utils/pager.go
+++ b/utils/pager.go
@@ -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
diff --git a/utils/pdf.go b/utils/pdf.go
deleted file mode 100644
index ce2602bf..00000000
--- a/utils/pdf.go
+++ /dev/null
@@ -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
-}
diff --git a/utils/pdf_windows.go b/utils/pdf_windows.go
new file mode 100644
index 00000000..d9199447
--- /dev/null
+++ b/utils/pdf_windows.go
@@ -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
+
+}
diff --git a/views/book/index.tpl b/views/book/index.tpl
index 8c28610a..48773686 100644
--- a/views/book/index.tpl
+++ b/views/book/index.tpl
@@ -58,9 +58,9 @@
diff --git a/views/document/export.tpl b/views/document/export.tpl
new file mode 100644
index 00000000..c86dd1c4
--- /dev/null
+++ b/views/document/export.tpl
@@ -0,0 +1,45 @@
+
+
+
+
+
+
+
+ {{.Model.BookName}} - Powered by MinDoc
+
+
+
+
+
+
+
+
+
+ {{if eq .Model.Editor "markdown"}}
+
+ {{else}}
+
+ {{end}}
+
+
+
+
+
+
+
+
+
+
+
{{.Lists.DocumentName}}
+
+ {{str2html .Lists.Release}}
+
+
+
+
+
+
+
\ No newline at end of file