Skip to content

Commit

Permalink
🐛 ebook fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yann0917 committed Jul 18, 2022
1 parent b3a2726 commit c4ab3af
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 56 deletions.
52 changes: 32 additions & 20 deletions cmd/app/ebook.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app

import (
"fmt"

"github.com/yann0917/dedao-dl/services"
"github.com/yann0917/dedao-dl/utils"
)
Expand Down Expand Up @@ -42,35 +44,45 @@ func EbookPage(title, enID string) (pages *services.EbookPage, err error) {
return
}
var svgContent []string

for _, order := range info.BookInfo.Orders {
index, count, offset := 0, 20, 20000
pageList, err1 := getService().EbookPages(order.ChapterID, token.Token, index, count, offset)

index, count, offset := 0, 20, 0
svgList, err1 := generateEbookPages(order.ChapterID, token.Token, index, count, offset)
if err1 != nil {
err = err1
return
}
svgContent = append(svgContent, svgList...)

for _, item := range pageList.Pages {
svgContent = append(svgContent, item.Svg)
}

if !pageList.IsEnd {
index = count
count += 20
pageList, err1 = getService().EbookPages(order.ChapterID, token.Token, index, count, offset)
if err1 != nil {
err = err1
return
}
for _, item := range pageList.Pages {
svgContent = append(svgContent, item.Svg)
}
}
}
if len(svgContent) > 0 {
// utils.WriteFile(order.ChapterID+".svg", strings.Join(svgContent, "\n"))
utils.Svg2Html(title, svgContent)
}
return
}

func generateEbookPages(chapterID, token string, index, count, offset int) (svgList []string, err error) {
fmt.Printf("chapterID:%#v\n", chapterID)
pageList, err := getService().EbookPages(chapterID, token, index, count, offset)
if err != nil {
return
}

for _, item := range pageList.Pages {
svgList = append(svgList, item.Svg)
}
fmt.Printf("IsEnd:%#v\n", pageList.IsEnd)
if !pageList.IsEnd {
index = count
count += 20
list, err1 := generateEbookPages(chapterID, token, index, count, offset)
if err1 != nil {
err = err1
return
}

svgList = append(svgList, list...)
}
// utils.WriteFileWithTrunc(chapterID+".svg", strings.Join(svgList, "\n"))
return
}
10 changes: 7 additions & 3 deletions services/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ func (s *Service) reqEbookInfo(token string) (io.ReadCloser, error) {

// reqEbookPages 获取页面详情
func (s *Service) reqEbookPages(chapterID, token string, index, count, offset int) (io.ReadCloser, error) {
// A4纸的尺寸为210mm*297mm(宽*高),这是国际化组织的ISO216定义的标准尺寸。
// 设定的分辨率是72像素/英寸时,A4纸的尺寸的图像的像素是595×842,在ps中的大小为1.43M。
// 设定的分辨率是150像素/英寸时,A4纸的尺寸的图像的像素是1240×1754,在ps中的大小为6.22M。
// 设定的分辨率是300像素/英寸时,A4纸的尺寸的图像的像素是2479×3508,在ps中的大小为24.9M。
resp, err := s.client.R().
SetBodyJsonMarshal(map[string]interface{}{
"chapter_id": chapterID,
Expand All @@ -245,15 +249,15 @@ func (s *Service) reqEbookPages(chapterID, token string, index, count, offset in
"font_name": "pingfang",
"font_scale": 1,
"font_size": 20,
"height": 1123,
"height": 3508,
"line_height": "2em",
"margin_bottom": 60,
"margin_left": 30,
"margin_right": 30,
"margin_top": 60,
"paragraph_space": "1em",
"paragraph_space": "2em",
"platform": 1,
"width": 794,
"width": 2479,
},
"token": token,
}).
Expand Down
3 changes: 2 additions & 1 deletion utils/chromedp_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"os"
"testing"
)

Expand All @@ -11,6 +12,6 @@ func TestPrintToPdf(t *testing.T) {
if err != nil {
t.Fatal("PrintToPDF test is failure", err)
} else {
// os.Remove(filename)
os.Remove(filename)
}
}
94 changes: 62 additions & 32 deletions utils/svg2html.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"path/filepath"
"sort"
"strconv"
"strings"

"github.com/JoshVarga/svgparser"
Expand All @@ -15,8 +16,10 @@ import (
type HtmlEle struct {
X string `json:"x"`
Y string `json:"y"`
ID string `json:"id"`
Width string `json:"width"`
Height string `json:"height"`
Offset string `json:"offset"`
Href string `json:"href"`
Name string `json:"name"`
Style string `json:"style"`
Expand Down Expand Up @@ -51,7 +54,8 @@ func Svg2Html(title string, contents []string) (err error) {
return err1
}

lineContent := make(map[string][]HtmlEle)
lineContent := make(map[float64][]HtmlEle)
offset := ""

for _, children := range element.Children {

Expand All @@ -60,14 +64,6 @@ func Svg2Html(title string, contents []string) (err error) {
content := children.Content

if y, ok := attr["y"]; ok {
// cont += content
// if newline, ok := attr["newline"]; ok && newline == "true" && children.Name == "text" {
// cont += "<br/>"
// ele.Content = cont
// cont = ""
// } else {
// ele.Content = ""
// }
if children.Name == "text" {
ele.Content = content
} else {
Expand All @@ -80,6 +76,15 @@ func Svg2Html(title string, contents []string) (err error) {
ele.Width = attr["width"]
ele.Height = attr["height"]

// id &offset 设置标题 margin-left
if _, ok := attr["id"]; ok {
ele.ID = attr["id"]
if _, ok := attr["offset"]; ok {
offset = attr["offset"]
}
}
ele.Offset = offset

if _, ok := attr["href"]; ok && children.Name == "image" {
ele.Href = attr["href"]
} else {
Expand All @@ -90,37 +95,59 @@ func Svg2Html(title string, contents []string) (err error) {

if (children.Name == "text" && ele.Content != "") ||
children.Name == "image" {
lineContent[y] = append(lineContent[y], ele)
yInt, _ := strconv.ParseFloat(y, 64)
lineContent[yInt] = append(lineContent[yInt], ele)
}
}
}

keys := make([]string, 0, len(lineContent))
keys := make([]float64, 0, len(lineContent))
for k := range lineContent {
keys = append(keys, k)
}
sort.Strings(keys)
cont := ""
for _, item := range lineContent {
sort.Float64s(keys)

for _, v := range keys {
cont := ""
result += `
<p>`
for i, v := range item {
switch v.Name {
id := ""
if lineContent[v][0].ID != "" {
id = lineContent[v][0].ID
}

for i, item := range lineContent[v] {

style := item.Style
// if id != "" {
// style += " margin-left:" + item.Offset + "px;"
// }
w, h := 0.0, 0.0
w, _ = strconv.ParseFloat(item.Width, 64)
h, _ = strconv.ParseFloat(item.Height, 64)
// 794x1123
if w > 794 {
w = 794
}
if h > 1123 {
h = 1123
}
switch item.Name {
case "image":
result += `<img width="` + v.Width + `" height="` + v.Height + `" src="` + v.Href + `"/>`
result += `<img width="` + strconv.FormatFloat(w, 'f', 0, 64) + `" height="` + strconv.FormatFloat(h, 'f', 0, 64) + `" src="` + item.Href + `"/>`
case "text":
cont += v.Content
if i == len(item)-1 {
result += `<span style="` + v.Style + `">` + cont + `</span>`
cont += item.Content
if i == len(lineContent[v])-1 {
result += `<span id="` + id + `" style="` + style + `">` + cont + `</span>`
}
}
}
cont = ""
result += `</p>`
}
}
result += `</body>
</html>`

path, err := Mkdir(OutputDir, "Ebook")
if err != nil {
return err
Expand All @@ -130,10 +157,12 @@ func Svg2Html(title string, contents []string) (err error) {
if err != nil {
return err
}

fmt.Printf("正在生成文件:【\033[37;1m%s\033[0m】 ", fileName)
if err = WriteFileWithTrunc(fileName, result); err != nil {
fmt.Printf("\033[31;1m%s\033[0m\n", "失败"+err.Error())
return
}
fmt.Printf("\033[32;1m%s\033[0m\n", "完成")
err = Html2PDF(title)
return
}
Expand All @@ -159,32 +188,33 @@ func Html2PDF(filename string) (err error) {
return
}

fileName, err = FilePath(filePreName, "pdf", false)
if err != nil {
return err
}
fmt.Printf("正在生成文件:【\033[37;1m%s\033[0m】 ", fileName)

pdfg.AddPage(wkhtmltopdf.NewPageReader(bytes.NewReader(htmlfile)))
pdfg.Dpi.Set(300)
pdfg.NoCollate.Set(false)
pdfg.PageSize.Set(wkhtmltopdf.PageSizeA4)

pdfg.MarginBottom.Set(40)
pdfg.MarginLeft.Set(30)
pdfg.MarginTop.Set(20)
pdfg.MarginBottom.Set(20)
pdfg.MarginLeft.Set(20)

err = pdfg.Create()
if err != nil {
fmt.Printf("pdfg create err: %#v\n", err)
return
// log.Fatal(err)
}

fileName, err = FilePath(filePreName, "pdf", false)
if err != nil {
return err
}

// Write buffer contents to file on disk
err = pdfg.WriteFile(fileName)
if err != nil {
fmt.Printf("pdfg WriteFile err: %#v\n", err)
fmt.Printf("\033[31;1m%s\033[0m\n", "失败"+err.Error())
return
// log.Fatal(err)
}
fmt.Printf("\033[32;1m%s\033[0m\n", "完成")
return
}

0 comments on commit c4ab3af

Please sign in to comment.