converter.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. //Author:TruthHun
  2. //Email:[email protected]
  3. //Date:2018-01-21
  4. package converter
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "os/exec"
  13. "errors"
  14. "github.com/lifei6671/mindoc/utils/filetil"
  15. "github.com/lifei6671/mindoc/utils/ziptil"
  16. "github.com/lifei6671/mindoc/utils/cryptil"
  17. "sync"
  18. "html"
  19. )
  20. type Converter struct {
  21. BasePath string
  22. OutputPath string
  23. Config Config
  24. Debug bool
  25. GeneratedCover string
  26. ProcessNum int //并发的任务数量
  27. process chan func()
  28. limitChan chan bool
  29. }
  30. //目录结构
  31. type Toc struct {
  32. Id int `json:"id"`
  33. Link string `json:"link"`
  34. Pid int `json:"pid"`
  35. Title string `json:"title"`
  36. }
  37. //config.json文件解析结构
  38. type Config struct {
  39. Charset string `json:"charset"` //字符编码,默认utf-8编码
  40. Cover string `json:"cover"` //封面图片,或者封面html文件
  41. Timestamp string `json:"date"` //时间日期,如“2018-01-01 12:12:21”,其实是time.Time格式,但是直接用string就好
  42. Description string `json:"description"` //摘要
  43. Footer string `json:"footer"` //pdf的footer
  44. Header string `json:"header"` //pdf的header
  45. Identifier string `json:"identifier"` //即uuid,留空即可
  46. Language string `json:"language"` //语言,如zh、en、zh-CN、en-US等
  47. Creator string `json:"creator"` //作者,即author
  48. Publisher string `json:"publisher"` //出版单位
  49. Contributor string `json:"contributor"` //同Publisher
  50. Title string `json:"title"` //文档标题
  51. Format []string `json:"format"` //导出格式,可选值:pdf、epub、mobi
  52. FontSize string `json:"font_size"` //默认的pdf导出字体大小
  53. PaperSize string `json:"paper_size"` //页面大小
  54. MarginLeft string `json:"margin_left"` //PDF文档左边距,写数字即可,默认72pt
  55. MarginRight string `json:"margin_right"` //PDF文档左边距,写数字即可,默认72pt
  56. MarginTop string `json:"margin_top"` //PDF文档左边距,写数字即可,默认72pt
  57. MarginBottom string `json:"margin_bottom"` //PDF文档左边距,写数字即可,默认72pt
  58. More []string `json:"more"` //更多导出选项[PDF导出选项,具体参考:https://manual.calibre-ebook.com/generated/en/ebook-convert.html#pdf-output-options]
  59. Toc []Toc `json:"toc"` //目录
  60. ///////////////////////////////////////////
  61. Order []string `json:"-"` //这个不需要赋值
  62. }
  63. var (
  64. output = "output" //文档导出文件夹
  65. ebookConvert = "ebook-convert"
  66. )
  67. // 接口文档 https://manual.calibre-ebook.com/generated/en/ebook-convert.html#table-of-contents
  68. //根据json配置文件,创建文档转化对象
  69. func NewConverter(configFile string, debug ...bool) (converter *Converter, err error) {
  70. var (
  71. cfg Config
  72. basepath string
  73. db bool
  74. )
  75. if len(debug) > 0 {
  76. db = debug[0]
  77. }
  78. if cfg, err = parseConfig(configFile); err == nil {
  79. if basepath, err = filepath.Abs(filepath.Dir(configFile)); err == nil {
  80. //设置默认值
  81. if len(cfg.Timestamp) == 0 {
  82. cfg.Timestamp = time.Now().Format("2006-01-02 15:04:05")
  83. }
  84. if len(cfg.Charset) == 0 {
  85. cfg.Charset = "utf-8"
  86. }
  87. converter = &Converter{
  88. Config: cfg,
  89. BasePath: basepath,
  90. Debug: db,
  91. ProcessNum: 1,
  92. process: make(chan func(),4),
  93. limitChan: make(chan bool,1),
  94. }
  95. }
  96. }
  97. return
  98. }
  99. //执行文档转换
  100. func (convert *Converter) Convert() (err error) {
  101. if !convert.Debug { //调试模式下不删除生成的文件
  102. defer convert.converterDefer() //最后移除创建的多余而文件
  103. }
  104. if convert.process == nil{
  105. convert.process = make(chan func(),4)
  106. }
  107. if convert.limitChan == nil {
  108. if convert.ProcessNum <= 0 {
  109. convert.ProcessNum = 1
  110. }
  111. convert.limitChan = make(chan bool,convert.ProcessNum)
  112. for i := 0; i < convert.ProcessNum;i++{
  113. convert.limitChan <- true
  114. }
  115. }
  116. if err = convert.generateMimeType(); err != nil {
  117. return
  118. }
  119. if err = convert.generateMetaInfo(); err != nil {
  120. return
  121. }
  122. if err = convert.generateTocNcx(); err != nil { //生成目录
  123. return
  124. }
  125. if err = convert.generateSummary(); err != nil { //生成文档内目录
  126. return
  127. }
  128. if err = convert.generateTitlePage(); err != nil { //生成封面
  129. return
  130. }
  131. if err = convert.generateContentOpf(); err != nil { //这个必须是generate*系列方法的最后一个调用
  132. return
  133. }
  134. //将当前文件夹下的所有文件压缩成zip包,然后直接改名成content.epub
  135. f := filepath.Join(convert.OutputPath, "content.epub")
  136. os.Remove(f) //如果原文件存在了,则删除;
  137. if err = ziptil.Zip(convert.BasePath,f); err == nil {
  138. //创建导出文件夹
  139. os.Mkdir(convert.BasePath+"/"+output, os.ModePerm)
  140. if len(convert.Config.Format) > 0 {
  141. var errs []string
  142. go func(convert *Converter) {
  143. for _, v := range convert.Config.Format {
  144. fmt.Println("convert to " + v)
  145. switch strings.ToLower(v) {
  146. case "epub":
  147. convert.process <- func() {
  148. if err = convert.convertToEpub(); err != nil {
  149. errs = append(errs, err.Error())
  150. fmt.Println("转换EPUB文档失败:" + err.Error())
  151. }
  152. }
  153. case "mobi":
  154. convert.process <- func() {
  155. if err = convert.convertToMobi(); err != nil {
  156. errs = append(errs, err.Error())
  157. fmt.Println("转换MOBI文档失败:" + err.Error())
  158. }
  159. }
  160. case "pdf":
  161. convert.process <- func() {
  162. if err = convert.convertToPdf(); err != nil {
  163. fmt.Println("转换PDF文档失败:" + err.Error())
  164. errs = append(errs, err.Error())
  165. }
  166. }
  167. case "docx":
  168. convert.process <- func() {
  169. if err = convert.convertToDocx(); err != nil {
  170. fmt.Println("转换WORD文档失败:" + err.Error())
  171. errs = append(errs, err.Error())
  172. }
  173. }
  174. }
  175. }
  176. close(convert.process)
  177. }(convert)
  178. group := sync.WaitGroup{}
  179. for {
  180. action, isClosed := <-convert.process
  181. if action == nil && !isClosed {
  182. break;
  183. }
  184. group.Add(1)
  185. <- convert.limitChan
  186. go func(group *sync.WaitGroup) {
  187. action()
  188. group.Done()
  189. convert.limitChan <- true
  190. }(&group)
  191. }
  192. group.Wait()
  193. if len(errs) > 0 {
  194. err = errors.New(strings.Join(errs, "\n"))
  195. }
  196. } else {
  197. err = convert.convertToPdf()
  198. if err != nil {
  199. fmt.Println(err)
  200. }
  201. }
  202. } else {
  203. fmt.Println("压缩目录出错" + err.Error())
  204. }
  205. return
  206. }
  207. //删除生成导出文档而创建的文件
  208. func (this *Converter) converterDefer() {
  209. //删除不必要的文件
  210. os.RemoveAll(filepath.Join(this.BasePath, "META-INF"))
  211. os.RemoveAll(filepath.Join(this.BasePath, "content.epub"))
  212. os.RemoveAll(filepath.Join(this.BasePath, "mimetype"))
  213. os.RemoveAll(filepath.Join(this.BasePath, "toc.ncx"))
  214. os.RemoveAll(filepath.Join(this.BasePath, "content.opf"))
  215. os.RemoveAll(filepath.Join(this.BasePath, "titlepage.xhtml")) //封面图片待优化
  216. os.RemoveAll(filepath.Join(this.BasePath, "summary.html")) //文档目录
  217. }
  218. //生成metainfo
  219. func (this *Converter) generateMetaInfo() (err error) {
  220. xml := `<?xml version="1.0"?>
  221. <container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
  222. <rootfiles>
  223. <rootfile full-path="content.opf" media-type="application/oebps-package+xml"/>
  224. </rootfiles>
  225. </container>
  226. `
  227. folder := filepath.Join(this.BasePath, "META-INF")
  228. os.MkdirAll(folder, os.ModePerm)
  229. err = ioutil.WriteFile(filepath.Join(folder, "container.xml"), []byte(xml), os.ModePerm)
  230. return
  231. }
  232. //形成mimetyppe
  233. func (this *Converter) generateMimeType() (err error) {
  234. return ioutil.WriteFile(filepath.Join(this.BasePath, "mimetype"), []byte("application/epub+zip"), os.ModePerm)
  235. }
  236. //生成封面
  237. func (this *Converter) generateTitlePage() (err error) {
  238. if ext := strings.ToLower(filepath.Ext(this.Config.Cover)); !(ext == ".html" || ext == ".xhtml") {
  239. xml := `<?xml version='1.0' encoding='` + this.Config.Charset + `'?>
  240. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="` + this.Config.Language + `">
  241. <head>
  242. <meta http-equiv="Content-Type" content="text/html; charset=` + this.Config.Charset + `"/>
  243. <meta name="calibre:cover" content="true"/>
  244. <title>Cover</title>
  245. <style type="text/css" title="override_css">
  246. @page {padding: 0pt; margin:0pt}
  247. body { text-align: center; padding:0pt; margin: 0pt; }
  248. </style>
  249. </head>
  250. <body>
  251. <div>
  252. <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="100%" height="100%" viewBox="0 0 800 1068" preserveAspectRatio="none">
  253. <image width="800" height="1068" xlink:href="` + strings.TrimPrefix(this.Config.Cover, "./") + `"/>
  254. </svg>
  255. </div>
  256. </body>
  257. </html>
  258. `
  259. if err = ioutil.WriteFile(filepath.Join(this.BasePath, "titlepage.xhtml"), []byte(xml), os.ModePerm); err == nil {
  260. this.GeneratedCover = "titlepage.xhtml"
  261. }
  262. }
  263. return
  264. }
  265. //生成文档目录
  266. func (this *Converter) generateTocNcx() (err error) {
  267. ncx := `<?xml version='1.0' encoding='` + this.Config.Charset + `'?>
  268. <ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1" xml:lang="%v">
  269. <head>
  270. <meta content="4" name="dtb:depth"/>
  271. <meta content="calibre (2.85.1)" name="dtb:generator"/>
  272. <meta content="0" name="dtb:totalPageCount"/>
  273. <meta content="0" name="dtb:maxPageNumber"/>
  274. </head>
  275. <docTitle>
  276. <text>%v</text>
  277. </docTitle>
  278. <navMap>%v</navMap>
  279. </ncx>
  280. `
  281. codes, _ := this.tocToXml(0, 1)
  282. ncx = fmt.Sprintf(ncx, this.Config.Language, html.EscapeString(this.Config.Title), strings.Join(codes, ""))
  283. return ioutil.WriteFile(filepath.Join(this.BasePath, "toc.ncx"), []byte(ncx), os.ModePerm)
  284. }
  285. //生成文档目录,即summary.html
  286. func (this *Converter) generateSummary() (err error) {
  287. //目录
  288. summary := `<!DOCTYPE html>
  289. <html lang="` + this.Config.Language + `">
  290. <head>
  291. <meta charset="` + this.Config.Charset + `">
  292. <title>目录</title>
  293. <style>
  294. body{margin: 0px;padding: 0px;}h1{text-align: center;padding: 0px;margin: 0px;}ul,li{list-style: none;}
  295. a{text-decoration: none;color: #4183c4;text-decoration: none;font-size: 16px;line-height: 28px;}
  296. </style>
  297. </head>
  298. <body>
  299. <h1>目&nbsp;&nbsp;&nbsp;&nbsp;录</h1>
  300. %v
  301. </body>
  302. </html>`
  303. summary = fmt.Sprintf(summary, strings.Join(this.tocToSummary(0), ""))
  304. return ioutil.WriteFile(filepath.Join(this.BasePath, "summary.html"), []byte(summary), os.ModePerm)
  305. }
  306. //将toc转成toc.ncx文件
  307. func (this *Converter) tocToXml(pid, idx int) (codes []string, next_idx int) {
  308. var code string
  309. for _, toc := range this.Config.Toc {
  310. if toc.Pid == pid {
  311. code, idx = this.getNavPoint(toc, idx)
  312. codes = append(codes, code)
  313. for _, item := range this.Config.Toc {
  314. if item.Pid == toc.Id {
  315. code, idx = this.getNavPoint(item, idx)
  316. codes = append(codes, code)
  317. var code_arr []string
  318. code_arr, idx = this.tocToXml(item.Id, idx)
  319. codes = append(codes, code_arr...)
  320. codes = append(codes, `</navPoint>`)
  321. }
  322. }
  323. codes = append(codes, `</navPoint>`)
  324. }
  325. }
  326. next_idx = idx
  327. return
  328. }
  329. //将toc转成toc.ncx文件
  330. func (this *Converter) tocToSummary(pid int) (summarys []string) {
  331. summarys = append(summarys, "<ul>")
  332. for _, toc := range this.Config.Toc {
  333. if toc.Pid == pid {
  334. summarys = append(summarys, fmt.Sprintf(`<li><a href="%v">%v</a></li>`, toc.Link, html.EscapeString(toc.Title)))
  335. for _, item := range this.Config.Toc {
  336. if item.Pid == toc.Id {
  337. summarys = append(summarys, fmt.Sprintf(`<li><ul><li><a href="%v">%v</a></li>`, item.Link, html.EscapeString(item.Title)))
  338. summarys = append(summarys, "<li>")
  339. summarys = append(summarys, this.tocToSummary(item.Id)...)
  340. summarys = append(summarys, "</li></ul></li>")
  341. }
  342. }
  343. }
  344. }
  345. summarys = append(summarys, "</ul>")
  346. return
  347. }
  348. //生成navPoint
  349. func (this *Converter) getNavPoint(toc Toc, idx int) (navpoint string, nextidx int) {
  350. navpoint = `
  351. <navPoint id="id%v" playOrder="%v">
  352. <navLabel>
  353. <text>%v</text>
  354. </navLabel>
  355. <content src="%v"/>`
  356. navpoint = fmt.Sprintf(navpoint, toc.Id, idx, html.EscapeString(toc.Title), toc.Link)
  357. this.Config.Order = append(this.Config.Order, toc.Link)
  358. nextidx = idx + 1
  359. return
  360. }
  361. //生成content.opf文件
  362. //倒数第二步调用
  363. func (this *Converter) generateContentOpf() (err error) {
  364. var (
  365. guide string
  366. manifest string
  367. manifestArr []string
  368. spine string //注意:如果存在封面,则需要把封面放在第一个位置
  369. spineArr []string
  370. )
  371. meta := `<dc:title>%v</dc:title>
  372. <dc:contributor opf:role="bkp">%v</dc:contributor>
  373. <dc:publisher>%v</dc:publisher>
  374. <dc:description>%v</dc:description>
  375. <dc:language>%v</dc:language>
  376. <dc:creator opf:file-as="Unknown" opf:role="aut">%v</dc:creator>
  377. <meta name="calibre:timestamp" content="%v"/>
  378. `
  379. meta = fmt.Sprintf(meta, html.EscapeString(this.Config.Title), html.EscapeString(this.Config.Contributor), html.EscapeString(this.Config.Publisher), html.EscapeString(this.Config.Description), this.Config.Language, html.EscapeString(this.Config.Creator), this.Config.Timestamp)
  380. if len(this.Config.Cover) > 0 {
  381. meta = meta + `<meta name="cover" content="cover"/>`
  382. guide = `<reference href="titlepage.xhtml" title="Cover" type="cover"/>`
  383. manifest = fmt.Sprintf(`<item href="%v" id="cover" media-type="%v"/>`, this.Config.Cover, GetMediaType(filepath.Ext(this.Config.Cover)))
  384. spineArr = append(spineArr, `<itemref idref="titlepage"/>`)
  385. }
  386. if _, err := os.Stat(this.BasePath + "/summary.html"); err == nil {
  387. spineArr = append(spineArr, `<itemref idref="summary"/>`) //目录
  388. }
  389. //扫描所有文件
  390. if files, err := filetil.ScanFiles(this.BasePath); err == nil {
  391. basePath := strings.Replace(this.BasePath, "\\", "/", -1)
  392. for _, file := range files {
  393. if !file.IsDir {
  394. ext := strings.ToLower(filepath.Ext(file.Path))
  395. sourcefile := strings.TrimPrefix(file.Path, basePath+"/")
  396. id := "ncx"
  397. if ext != ".ncx" {
  398. if file.Name == "titlepage.xhtml" { //封面
  399. id = "titlepage"
  400. } else if file.Name == "summary.html" { //目录
  401. id = "summary"
  402. } else {
  403. id = cryptil.Md5Crypt(sourcefile)
  404. }
  405. }
  406. if mt := GetMediaType(ext); mt != "" { //不是封面图片,且media-type不为空
  407. if sourcefile != strings.TrimLeft(this.Config.Cover, "./") { //不是封面图片,则追加进来。封面图片前面已经追加进来了
  408. manifestArr = append(manifestArr, fmt.Sprintf(`<item href="%v" id="%v" media-type="%v"/>`, sourcefile, id, mt))
  409. }
  410. }
  411. } else {
  412. fmt.Println(file.Path)
  413. }
  414. }
  415. items := make(map[string]string)
  416. for _, link := range this.Config.Order {
  417. id := cryptil.Md5Crypt(link)
  418. if _, ok := items[id]; !ok { //去重
  419. items[id] = id
  420. spineArr = append(spineArr, fmt.Sprintf(`<itemref idref="%v"/>`, id))
  421. }
  422. }
  423. manifest = manifest + strings.Join(manifestArr, "\n")
  424. spine = strings.Join(spineArr, "\n")
  425. } else {
  426. return err
  427. }
  428. pkg := `<?xml version='1.0' encoding='` + this.Config.Charset + `'?>
  429. <package xmlns="http://www.idpf.org/2007/opf" unique-identifier="uuid_id" version="2.0">
  430. <metadata xmlns:opf="http://www.idpf.org/2007/opf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:calibre="http://calibre.kovidgoyal.net/2009/metadata">
  431. %v
  432. </metadata>
  433. <manifest>
  434. %v
  435. </manifest>
  436. <spine toc="ncx">
  437. %v
  438. </spine>
  439. %v
  440. </package>
  441. `
  442. if len(guide) > 0 {
  443. guide = `<guide>` + guide + `</guide>`
  444. }
  445. pkg = fmt.Sprintf(pkg, meta, manifest, spine, guide)
  446. return ioutil.WriteFile(filepath.Join(this.BasePath, "content.opf"), []byte(pkg), os.ModePerm)
  447. }
  448. //转成epub
  449. func (this *Converter) convertToEpub() (err error) {
  450. args := []string{
  451. filepath.Join(this.OutputPath, "content.epub"),
  452. filepath.Join(this.OutputPath, output, "book.epub"),
  453. }
  454. //cmd := exec.Command(ebookConvert, args...)
  455. //
  456. //if this.Debug {
  457. // fmt.Println(cmd.Args)
  458. //}
  459. //fmt.Println("正在转换EPUB文件", args[0])
  460. //return cmd.Run()
  461. return filetil.CopyFile(args[0],args[1])
  462. }
  463. //转成mobi
  464. func (this *Converter) convertToMobi() (err error) {
  465. args := []string{
  466. filepath.Join(this.OutputPath, "content.epub"),
  467. filepath.Join(this.OutputPath, output, "book.mobi"),
  468. }
  469. cmd := exec.Command(ebookConvert, args...)
  470. if this.Debug {
  471. fmt.Println(cmd.Args)
  472. }
  473. fmt.Println("正在转换 MOBI 文件", args[0])
  474. return cmd.Run()
  475. }
  476. //转成pdf
  477. func (this *Converter) convertToPdf() (err error) {
  478. args := []string{
  479. filepath.Join(this.OutputPath, "content.epub"),
  480. filepath.Join(this.OutputPath, output, "book.pdf"),
  481. }
  482. //页面大小
  483. if len(this.Config.PaperSize) > 0 {
  484. args = append(args, "--paper-size", this.Config.PaperSize)
  485. }
  486. //文字大小
  487. if len(this.Config.FontSize) > 0 {
  488. args = append(args, "--pdf-default-font-size", this.Config.FontSize)
  489. }
  490. //header template
  491. if len(this.Config.Header) > 0 {
  492. args = append(args, "--pdf-header-template", this.Config.Header)
  493. }
  494. //footer template
  495. if len(this.Config.Footer) > 0 {
  496. args = append(args, "--pdf-footer-template",this.Config.Footer)
  497. }
  498. if strings.Count(this.Config.MarginLeft,"") > 0 {
  499. args = append(args, "--pdf-page-margin-left", this.Config.MarginLeft)
  500. }
  501. if strings.Count(this.Config.MarginTop,"") > 0 {
  502. args = append(args, "--pdf-page-margin-top", this.Config.MarginTop)
  503. }
  504. if strings.Count(this.Config.MarginRight,"") > 0 {
  505. args = append(args, "--pdf-page-margin-right", this.Config.MarginRight)
  506. }
  507. if strings.Count(this.Config.MarginBottom,"") > 0 {
  508. args = append(args, "--pdf-page-margin-bottom", this.Config.MarginBottom)
  509. }
  510. //更多选项
  511. if len(this.Config.More) > 0 {
  512. args = append(args, this.Config.More...)
  513. }
  514. cmd := exec.Command(ebookConvert, args...)
  515. if this.Debug {
  516. fmt.Println(cmd.Args)
  517. }
  518. fmt.Println("正在转换 PDF 文件", args[0])
  519. return cmd.Run()
  520. }
  521. // 转成word
  522. func (this *Converter) convertToDocx() (err error) {
  523. args := []string{
  524. filepath.Join(this.OutputPath , "content.epub"),
  525. filepath.Join(this.OutputPath , output , "book.docx"),
  526. }
  527. args = append(args, "--docx-no-toc")
  528. //页面大小
  529. if len(this.Config.PaperSize) > 0 {
  530. args = append(args, "--docx-page-size", this.Config.PaperSize)
  531. }
  532. if len(this.Config.MarginLeft) > 0 {
  533. args = append(args, "--docx-page-margin-left", this.Config.MarginLeft)
  534. }
  535. if len(this.Config.MarginTop) > 0 {
  536. args = append(args, "--docx-page-margin-top", this.Config.MarginTop)
  537. }
  538. if len(this.Config.MarginRight) > 0 {
  539. args = append(args, "--docx-page-margin-right", this.Config.MarginRight)
  540. }
  541. if len(this.Config.MarginBottom) > 0 {
  542. args = append(args, "--docx-page-margin-bottom", this.Config.MarginBottom)
  543. }
  544. cmd := exec.Command(ebookConvert, args...)
  545. if this.Debug {
  546. fmt.Println(cmd.Args)
  547. }
  548. fmt.Println("正在转换 DOCX 文件", args[0])
  549. return cmd.Run()
  550. }