尝试生成pdf文件

pull/39/head
Minho 2017-05-05 18:02:52 +08:00
parent 906191624c
commit 4b264670a7
9 changed files with 136 additions and 36 deletions

View File

@ -55,51 +55,68 @@ func RegisterModel() {
//初始化数据 //初始化数据
func Initialization() { func Initialization() {
options := []models.Option { o := orm.NewOrm()
{ OptionName: "ENABLED_CAPTCHA", OptionValue: "false", OptionTitle:"是否启用验证码"},
{ OptionName: "ENABLED_REGISTER",OptionValue:"false",OptionTitle:"是否启用注册"}, _,err := o.Raw(`INSERT INTO md_options (option_title, option_name, option_value) SELECT '是否启用注册','ENABLED_REGISTER','false' FROM dual WHERE NOT exists(SELECT * FROM md_options WHERE option_name = 'ENABLED_REGISTER');`).Exec()
{ OptionName: "ENABLE_ANONYMOUS" , OptionValue:"false", OptionTitle:"启用匿名访问"},
{ OptionName: "SITE_NAME", OptionValue:"MinDoc", OptionTitle: "站点名称"}, if err != nil {
panic("ENABLED_REGISTER => " + err.Error())
os.Exit(1)
} }
_,err = o.Raw(`INSERT INTO md_options (option_title, option_name, option_value) SELECT '是否启用验证码','ENABLED_CAPTCHA','false' FROM dual WHERE NOT exists(SELECT * FROM md_options WHERE option_name = 'ENABLED_CAPTCHA');`).Exec()
if err != nil {
panic("ENABLED_CAPTCHA => " + err.Error())
os.Exit(1)
}
_,err = o.Raw(`INSERT INTO md_options (option_title, option_name, option_value) SELECT '启用匿名访问','ENABLE_ANONYMOUS','true' FROM dual WHERE NOT exists(SELECT * FROM md_options WHERE option_name = 'ENABLE_ANONYMOUS');`).Exec()
if err := models.NewOption().InsertMulti(options...);err != nil { if err != nil {
panic("Option.InsertMulti => " + err.Error()) panic("ENABLE_ANONYMOUS => " + err.Error())
os.Exit(1)
}
_,err = o.Raw(`INSERT INTO md_options (option_title, option_name, option_value) SELECT '站点名称','SITE_NAME','MinDoc' FROM dual WHERE NOT exists(SELECT * FROM md_options WHERE option_name = 'SITE_NAME');`).Exec()
if err != nil {
panic("SITE_NAME => " + err.Error())
os.Exit(1) os.Exit(1)
} }
member := models.NewMember() member,err := models.NewMember().FindByFieldFirst("account","admin")
member.Account = "admin" if err == orm.ErrNoRows {
member.Avatar = "/static/images/headimgurl.jpg"
member.Password = "123456"
member.Role = 0
member.Email = "admin@iminho.me"
if err := member.Add();err != nil { member.Account = "admin"
panic("Member.Add => " + err.Error()) member.Avatar = "/static/images/headimgurl.jpg"
os.Exit(0) member.Password = "123456"
} member.Role = 0
member.Email = "admin@iminho.me"
book := models.NewBook() if err := member.Add(); err != nil {
panic("Member.Add => " + err.Error())
os.Exit(0)
}
book.MemberId = member.MemberId book := models.NewBook()
book.BookName = "MinDoc演示项目"
book.Status = 0
book.Description = "这是一个MinDoc演示项目该项目是由系统初始化时自动创建。"
book.CommentCount = 0
book.PrivatelyOwned = 0
book.CommentStatus = "closed"
book.Identify = "mindoc"
book.DocCount = 0
book.CommentCount = 0
book.Version = time.Now().Unix()
book.Cover = conf.GetDefaultCover()
book.Editor = "markdown"
book.Theme = "default"
if err := book.Insert(); err != nil { book.MemberId = member.MemberId
panic("Book.Insert => " + err.Error()) book.BookName = "MinDoc演示项目"
os.Exit(0) book.Status = 0
book.Description = "这是一个MinDoc演示项目该项目是由系统初始化时自动创建。"
book.CommentCount = 0
book.PrivatelyOwned = 0
book.CommentStatus = "closed"
book.Identify = "mindoc"
book.DocCount = 0
book.CommentCount = 0
book.Version = time.Now().Unix()
book.Cover = conf.GetDefaultCover()
book.Editor = "markdown"
book.Theme = "default"
if err := book.Insert(); err != nil {
panic("Book.Insert => " + err.Error())
os.Exit(0)
}
} }
} }
@ -128,7 +145,7 @@ func RegisterLogger() {
func RegisterCommand() { func RegisterCommand() {
if _,err := os.Stat("install.lock"); os.IsNotExist(err){ if _,err := os.Stat("install.lock"); os.IsNotExist(err){
err = orm.RunSyncdb("default",true,false) err = orm.RunSyncdb("default",false,true)
if err == nil { if err == nil {
Initialization() Initialization()
f, _ := os.Create("install.lock") f, _ := os.Create("install.lock")
@ -139,6 +156,7 @@ func RegisterCommand() {
} }
} }
CheckUpdate()
} }
func RegisterFunction() { func RegisterFunction() {

52
commands/update.go 100644
View File

@ -0,0 +1,52 @@
package commands
import (
"os"
"net/http"
"github.com/astaxie/beego"
"encoding/json"
"io/ioutil"
"fmt"
)
func Update() {
if len(os.Args) > 2 && os.Args[1] == "update" {
os.Exit(0)
}
}
//检查最新版本.
func CheckUpdate() {
if len(os.Args) >= 2 && os.Args[1] == "version" {
resp, err := http.Get("https://api.github.com/repos/lifei6671/godoc/tags")
if err != nil {
beego.Error("CheckUpdate => ", err)
os.Exit(1)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
beego.Error("CheckUpdate => ", err)
os.Exit(1)
}
var result []*struct {
Name string `json:"name"`
}
err = json.Unmarshal(body, &result)
if err != nil {
beego.Error("CheckUpdate => ", err)
os.Exit(1)
}
fmt.Println("MinDoc last version => ",result[0].Name)
os.Exit(0)
}
}

View File

@ -12,6 +12,7 @@ import (
"os" "os"
"github.com/lifei6671/godoc/controllers" "github.com/lifei6671/godoc/controllers"
"github.com/lifei6671/godoc/conf" "github.com/lifei6671/godoc/conf"
"github.com/lifei6671/godoc/utils"
) )
var ( var (
@ -36,6 +37,7 @@ func main() {
beego.ErrorController(&controllers.ErrorController{}) beego.ErrorController(&controllers.ErrorController{})
utils.ConverterPdf("a.pdf",nil)
beego.Run() beego.Run()
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

28
utils/pdf.go 100644
View File

@ -0,0 +1,28 @@
package utils
import (
"github.com/signintech/gopdf"
"github.com/astaxie/beego"
)
func ConverterPdf(output string,htmlList map[string]string) error {
pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}})
pdf.AddPage()
err := pdf.AddTTFFont("HDZB_5", "./static/pdf-fonts/msyh.ttf")
if err != nil {
beego.Error("ConverterPdf => ",err)
return err
}
err = pdf.SetFont("HDZB_5", "", 14)
if err != nil {
beego.Error("ConverterPdf => " , err)
return err
}
pdf.Cell(nil, "您好")
pdf.WritePdf(output)
return nil
}