From 825fda55443aa51669804818665347fd97520638 Mon Sep 17 00:00:00 2001
From: lealife <lifephp@gmail.com>
Date: Thu, 27 Jul 2017 20:00:53 +0800
Subject: [PATCH] windows export pdf

---
 app/controllers/NoteController.go       | 25 +++++++++++++++++++++----
 app/views/admin/setting/export_pdf.html |  2 +-
 bin/run.bat                             |  8 ++++----
 3 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/app/controllers/NoteController.go b/app/controllers/NoteController.go
index 4288cca..1cb434e 100644
--- a/app/controllers/NoteController.go
+++ b/app/controllers/NoteController.go
@@ -447,7 +447,11 @@ func (c Note) ExportPdf(noteId string) revel.Result {
 	binPath := configService.GetGlobalStringConfig("exportPdfBinPath")
 	// 默认路径
 	if binPath == "" {
-		binPath = "/usr/local/bin/wkhtmltopdf"
+		if runtime.GOOS == "windows" {
+			binPath = `C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe`
+		} else {
+			binPath = "/usr/local/bin/wkhtmltopdf"
+		}
 	}
 
 	url := configService.GetSiteUrl() + "/note/toPdf?noteId=" + noteId + "&appKey=" + appKey
@@ -457,15 +461,28 @@ func (c Note) ExportPdf(noteId string) revel.Result {
 	// http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html
 	// wkhtmltopdf参数大全
 	var cc string
+	// var cc []string
+	var ccWindows []string
 	if note.IsMarkdown {
 		cc = binPath + " --lowquality --window-status done \"" + url + "\"  \"" + path + "\"" //  \"" + cookieDomain + "\" \"" + cookieName + "\" \"" + cookieValue + "\""
+		// cc = []string{binPath, "--lowquality", "--window-status", "done", "\"" + url + "\"", "\"" + path + "\""}
+		ccWindows = []string{"/C", binPath, "--lowquality", "--window-status", "done", url, path}
 	} else {
 		cc = binPath + " --lowquality \"" + url + "\"  \"" + path + "\"" //  \"" + cookieDomain + "\" \"" + cookieName + "\" \"" + cookieValue + "\""
+		// cc = []string{binPath, "--lowquality", "\"" + url + "\"", "\"" + path + "\""}
+		ccWindows = []string{"/C", binPath, "--lowquality", url, path}
 	}
 
-	cmd := exec.Command("/bin/sh", "-c", cc)
-	if runtime.GOARCH == "windows" {
-		cmd = exec.Command("cmd", "/C", cc)
+	var cmd *exec.Cmd
+
+	// fmt.Println("-------1", runtime.GOOS, ccWindows)
+	if runtime.GOOS == "windows" {
+		fmt.Println(ccWindows)
+		// cmd = exec.Command("cmd", ccWindows...)
+		cmd = exec.Command(ccWindows[1], ccWindows[2:]...)
+	} else {
+		fmt.Println(cc)
+		cmd = exec.Command("/bin/sh", "-c", cc)
 	}
 	_, err := cmd.Output()
 	if err != nil {
diff --git a/app/views/admin/setting/export_pdf.html b/app/views/admin/setting/export_pdf.html
index 0a27d13..0686eb3 100644
--- a/app/views/admin/setting/export_pdf.html
+++ b/app/views/admin/setting/export_pdf.html
@@ -12,7 +12,7 @@
 					<input type="text" class="form-control" placeholder="/usr/local/bin/wkhtmltopdf" name="path" value="{{.str.exportPdfBinPath}}">
 					Leanote use <a target="_blank" href="http://wkhtmltopdf.org">wkhtmltopdf</a> to export pdf. You should install it on your server.
 					<br />
-					Please input the path that wkhtmltopdf installed, e.g. <code>/usr/local/bin/wkhtmltopdf</code>
+					Please input the path that wkhtmltopdf installed, e.g. <code>/usr/local/bin/wkhtmltopdf</code> on mac os/linux or <code><C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe/code> on windows.
 				</div>
 			</div>
 
diff --git a/bin/run.bat b/bin/run.bat
index cdb3831..4a47499 100644
--- a/bin/run.bat
+++ b/bin/run.bat
@@ -4,13 +4,13 @@ cd..
 set SCRIPTPATH=%cd%
 
 : top src directory
-set path="%SCRIPTPATH%\bin\src\github.com\leanote"
+set leanotePath="%SCRIPTPATH%\bin\src\github.com\leanote"
 
-if not exist "%path%" mkdir "%path%"
+if not exist "%leanotePath%" mkdir "%leanotePath%"
 
 : create software link
-if exist "%path%\leanote" del /Q "%path%\leanote"
-mklink /D "%path%\leanote"  %SCRIPTPATH%
+if exist "%leanotePath%\leanote" del /Q "%leanotePath%\leanote"
+mklink /D "%leanotePath%\leanote"  %SCRIPTPATH%
 
 : set GOPATH
 set GOPATH="%SCRIPTPATH%\bin"