-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
209 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"runtime" | ||
|
||
"github.com/SebastiaanKlippert/go-wkhtmltopdf" | ||
) | ||
|
||
type PdfOption struct { | ||
FileName string | ||
CoverPath string | ||
PageSize string | ||
Toc bool | ||
} | ||
|
||
func (p *PdfOption) GenPdf(buf *bytes.Buffer) (err error) { | ||
pdfg, _ := wkhtmltopdf.NewPDFGenerator() | ||
page := wkhtmltopdf.NewPageReader(buf) | ||
page.FooterFontSize.Set(10) | ||
page.FooterRight.Set("[page]") | ||
page.DisableSmartShrinking.Set(true) | ||
|
||
page.EnableLocalFileAccess.Set(true) | ||
pdfg.AddPage(page) | ||
|
||
if p.CoverPath != "" { | ||
pdfg.Cover.EnableLocalFileAccess.Set(true) | ||
|
||
if runtime.GOOS == "windows" { | ||
pdfg.Cover.Input = p.CoverPath | ||
} else { | ||
pdfg.Cover.Input = "file://" + p.CoverPath | ||
} | ||
} | ||
|
||
pdfg.Dpi.Set(300) | ||
if p.Toc { | ||
pdfg.TOC.Include = true | ||
pdfg.TOC.TocHeaderText.Set("目 录") | ||
pdfg.TOC.HeaderFontSize.Set(18) | ||
|
||
pdfg.TOC.TocLevelIndentation.Set(15) | ||
pdfg.TOC.TocTextSizeShrink.Set(0.9) | ||
pdfg.TOC.DisableDottedLines.Set(false) | ||
pdfg.TOC.EnableTocBackLinks.Set(true) | ||
} | ||
|
||
pdfg.PageSize.Set(wkhtmltopdf.PageSizeA4) | ||
|
||
pdfg.MarginTop.Set(15) | ||
pdfg.MarginBottom.Set(15) | ||
pdfg.MarginLeft.Set(15) | ||
pdfg.MarginRight.Set(15) | ||
err = pdfg.Create() | ||
if err != nil { | ||
fmt.Printf("pdfg create err: %#v\n", err) | ||
return | ||
} | ||
|
||
// Write buffer contents to file on disk | ||
err = pdfg.WriteFile(p.FileName) | ||
if err != nil { | ||
fmt.Printf("\033[31;1m%s\033[0m\n", "失败"+err.Error()) | ||
return | ||
} | ||
fmt.Printf("\033[32;1m%s\033[0m\n", "完成") | ||
if p.CoverPath != "" { | ||
err = os.Remove(p.CoverPath) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/gomarkdown/markdown" | ||
"github.com/gomarkdown/markdown/html" | ||
"github.com/gomarkdown/markdown/parser" | ||
) | ||
|
||
func Md2Pdf(path, title string, md []byte) (err error) { | ||
if err != nil { | ||
return err | ||
} | ||
title = FileName(title, "pdf") | ||
filePreName := filepath.Join(path, title) | ||
fileName, err := FilePath(filePreName, "", false) | ||
if err != nil { | ||
return err | ||
} | ||
buf := new(bytes.Buffer) | ||
|
||
h := mdToHTML(md) | ||
article := genHeadHtml() + string(h) + ` | ||
</body> | ||
</html>` | ||
buf.Write([]byte(article)) | ||
pdf := PdfOption{ | ||
FileName: fileName, | ||
PageSize: "A4", | ||
Toc: false, | ||
} | ||
fmt.Printf("正在生成文件:【\033[37;1m%s\033[0m】 ", title) | ||
err = pdf.GenPdf(buf) | ||
return | ||
} | ||
|
||
func genHeadHtml() (result string) { | ||
result = `<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
<style> | ||
@font-face { font-family: "FZFangSong-Z02"; src:local("FZFangSong-Z02"), url("https://imgcdn.umiwi.com/ttf/fangzhengfangsong_gbk.ttf"); } | ||
@font-face { font-family: "FZKai-Z03"; src:local("FZFangSong-Z02S"), url("https://imgcdn.umiwi.com/ttf/0315911813008928624065681028886857980055.ttf"); } | ||
@font-face { font-family: "FZKai-Z03"; src:local("FZKai-Z03"), url("https://imgcdn.umiwi.com/ttf/fangzhengkaiti_gbk.ttf"); } | ||
@font-face { font-family: "PingFang SC"; src:local("PingFang SC"); } | ||
@font-face { font-family: "DeDaoJinKai"; src:local("DeDaoJinKai"), url("https://imgcdn.umiwi.com/ttf/dedaojinkaiw03.ttf");} | ||
@font-face { font-family: "Source Code Pro"; src:local("Source Code Pro"), url("https://imgcdn.umiwi.com/ttf/0315911806889993935644188722660020367983.ttf"); } | ||
table, tr, td, th, tbody, thead, tfoot {page-break-inside: avoid !important;} | ||
img { page-break-inside: avoid; max-width: 100% !important;} | ||
img.epub-footnote { padding-right:5px;} | ||
body {font-family: PingFang SC,Arial,sans-serif,Source Code Pro;color: #333;text-align: left;line-height: 1.8;} | ||
em {font-style: normal;} | ||
h2>code { background-color: rgb(255, 96, 2);padding: 0.5%;border-radius: 10%;color: white;} | ||
p>em {color: rgb(255, 96, 2);} | ||
</style> | ||
</head> | ||
<body> | ||
` | ||
return | ||
} | ||
|
||
func mdToHTML(md []byte) []byte { | ||
// create markdown parser with extensions | ||
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock | ||
p := parser.NewWithExtensions(extensions) | ||
doc := p.Parse(md) | ||
|
||
// create HTML renderer with extensions | ||
htmlFlags := html.CommonFlags | html.HrefTargetBlank | ||
opts := html.RendererOptions{Flags: htmlFlags} | ||
renderer := html.NewRenderer(opts) | ||
|
||
return markdown.Render(doc, renderer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters