Browse Source

优化导出

Minho 8 years ago
parent
commit
45ef30c4d9
2 changed files with 28 additions and 13 deletions
  1. 9 1
      controllers/book.go
  2. 19 12
      controllers/document.go

+ 9 - 1
controllers/book.go

@@ -523,7 +523,15 @@ func (c *BookController) Release() {
 		c.JsonResult(6003,"权限不足")
 	}
 
-	go models.NewDocument().ReleaseContent(book.BookId)
+	go func(identify string) {
+		models.NewDocument().ReleaseContent(book.BookId)
+		pdfpath := "cache/" + identify + ".pdf"
+
+		if _,err := os.Stat(pdfpath); os.IsExist(err){
+			os.Remove(pdfpath)
+		}
+
+	}(identify)
 
 	c.JsonResult(0,"发布任务已推送到任务队列,稍后将在后台执行。")
 }

+ 19 - 12
controllers/document.go

@@ -593,6 +593,15 @@ func (c *DocumentController) Export() {
 	}
 
 	if output == "pdf" {
+
+		exe := beego.AppConfig.String("wkhtmltopdf")
+
+		if exe == "" {
+			c.TplName = "errors/error.tpl";
+			c.Data["ErrorMessage"] = "没有配置PDF导出程序"
+			c.Data["ErrorCode"] = 50010
+			return
+		}
 		dpath := "cache/" + book.Identify
 
 		os.MkdirAll(dpath, 0766)
@@ -606,23 +615,21 @@ func (c *DocumentController) Export() {
 		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)
+		if _,err := os.Stat(pdfpath); os.IsNotExist(err){
+			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()
 	}