🔥 PDF export supports setting footer template https://github.com/siyuan-note/siyuan/issues/7478

Use frontend instead of backend impl
This commit is contained in:
Liang Ding 2023-04-02 10:39:53 +08:00
parent cd0fb32d38
commit d544fec54e
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 0 additions and 100 deletions

View file

@ -29,7 +29,6 @@ import (
"sort"
"strconv"
"strings"
"text/template"
"time"
"unicode/utf8"
@ -41,7 +40,6 @@ import (
"github.com/88250/lute/render"
"github.com/88250/pdfcpu/pkg/api"
"github.com/88250/pdfcpu/pkg/pdfcpu"
"github.com/Masterminds/sprig/v3"
"github.com/emirpasic/gods/sets/hashset"
"github.com/emirpasic/gods/stacks/linkedliststack"
"github.com/imroc/req/v3"
@ -715,7 +713,6 @@ func ProcessPDF(id, p string, merge, removeAssets bool) (err error) {
processPDFBookmarks(pdfCtx, headings)
processPDFLinkEmbedAssets(pdfCtx, assetDests, removeAssets)
processPDFFooter(pdfCtx)
pdfcpu.VersionStr = "SiYuan v" + util.Ver
if writeErr := api.WriteContextFile(pdfCtx, p); nil != writeErr {
@ -975,50 +972,6 @@ func processPDFLinkEmbedAssets(pdfCtx *pdfcpu.Context, assetDests []string, remo
}
}
func processPDFFooter(pdfCtx *pdfcpu.Context) {
templateContent := strings.TrimSpace(Conf.Export.PDFFooter)
if "" == templateContent {
return
}
footerTpl, err := template.New("").Funcs(sprig.TxtFuncMap()).Parse(templateContent)
if nil != err {
logging.LogErrorf("parse pdf footer template failed: %s", err)
return
}
buf := &bytes.Buffer{}
buf.Grow(4096)
err = footerTpl.Execute(buf, nil)
if nil != err {
logging.LogErrorf("render pdf footer template failed: %s", err)
return
}
footer := buf.String()
fontName := util.InstallPDFFonts()
pos := "bc"
desc := fmt.Sprintf("font:%s, points:8, sc:1 abs, pos:%s, off:10 10, fillc: 0.5 0.5 0.5, rot:0", fontName, pos)
footer = strings.ReplaceAll(footer, "%pages", strconv.Itoa(pdfCtx.PageCount))
m := map[int]*pdfcpu.Watermark{}
for i := 1; i <= pdfCtx.PageCount; i++ {
text := strings.ReplaceAll(footer, "%page", strconv.Itoa(i))
wm, watermarkErr := api.TextWatermark(text, desc, true, false, pdfcpu.POINTS)
if nil != watermarkErr {
logging.LogErrorf("add pdf footer failed: %s", watermarkErr)
return
}
m[i] = wm
}
if watermarkErr := pdfCtx.AddWatermarksMap(m); nil != watermarkErr {
logging.LogErrorf("add pdf footer failed: %s", watermarkErr)
return
}
}
func ExportStdMarkdown(id string) string {
tree, err := loadTreeByBlockID(id)
if nil != err {

View file

@ -22,12 +22,7 @@ import (
"strings"
"github.com/88250/gulu"
"github.com/88250/pdfcpu/pkg/api"
"github.com/88250/pdfcpu/pkg/font"
"github.com/ConradIrwin/font/sfnt"
"github.com/adrg/strutil"
"github.com/adrg/strutil/metrics"
"github.com/adrg/sysfont"
"github.com/flopp/go-findfont"
"github.com/siyuan-note/logging"
ttc "golang.org/x/image/font/sfnt"
@ -35,54 +30,6 @@ import (
"golang.org/x/text/transform"
)
var (
preferredPDFWatermarkFonts = []string{"MicrosoftYaHei", "SimSun", "ArialUnicode", "Xihei", "Heiti", "AquaHiraKaku", "AppleGothic", "Helvetica"}
)
func InstallPDFFonts() string {
names := font.UserFontNames()
if 0 < len(names) {
return getPreferredPDFWatermarkFont(names)
}
finder := sysfont.NewFinder(&sysfont.FinderOpts{Extensions: []string{".ttf", ".ttc"}})
var fontPaths []string
for _, preferredFont := range preferredPDFWatermarkFonts {
f := finder.Match(preferredFont)
if nil != f {
fontPaths = append(fontPaths, f.Filename)
}
}
logging.LogInfof("installing fonts [%s]", strings.Join(fontPaths, ", "))
if err := api.InstallFonts(fontPaths); nil != err {
logging.LogErrorf("install font [%s] failed: %s", strings.Join(fontPaths, ", "), err)
return getPreferredPDFWatermarkFont(names)
}
names = font.UserFontNames()
return getPreferredPDFWatermarkFont(names)
}
func getPreferredPDFWatermarkFont(userFontNames []string) (ret string) {
ret = "Helvetica"
if 1 > len(userFontNames) {
return
}
var score float64
for _, preferredFont := range preferredPDFWatermarkFonts {
for _, userFont := range userFontNames {
if tmp := strutil.Similarity(preferredFont, userFont, &metrics.JaroWinkler{CaseSensitive: false}); score < tmp {
ret = preferredFont
score = tmp
}
}
}
logging.LogInfof("preferred PDF font [%s]", ret)
return
}
func GetSysFonts(currentLanguage string) (ret []string) {
fonts := loadFonts(currentLanguage)
ret = []string{}