fix pdf
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -26,6 +26,7 @@ Thumbs.db
|
||||
tmp/
|
||||
temp/
|
||||
console
|
||||
worker
|
||||
|
||||
# 依赖目录
|
||||
vendor/
|
||||
|
||||
21
Dockerfile
21
Dockerfile
@@ -31,13 +31,6 @@ RUN BUILD_TIME=${BUILD_TIME:-$(date -u +"%Y-%m-%dT%H:%M:%SZ")} && \
|
||||
-o tyapi-server \
|
||||
cmd/api/main.go
|
||||
|
||||
# 创建字体文件 tar 包(如果字体文件存在)
|
||||
RUN if [ -d internal/shared/pdf/fonts ] && [ "$(ls -A internal/shared/pdf/fonts 2>/dev/null)" ]; then \
|
||||
tar czf /tmp/fonts.tar.gz -C internal/shared/pdf/fonts .; \
|
||||
else \
|
||||
touch /tmp/fonts.tar.gz; \
|
||||
fi
|
||||
|
||||
# 第二阶段:运行阶段
|
||||
FROM alpine:3.19
|
||||
|
||||
@@ -60,18 +53,8 @@ COPY --from=builder /app/tyapi-server .
|
||||
COPY config.yaml .
|
||||
COPY configs/ ./configs/
|
||||
|
||||
# 复制字体文件(PDF生成需要)
|
||||
RUN mkdir -p ./internal/shared/pdf/fonts
|
||||
# 从 builder 阶段复制字体 tar 包并解压(如果存在字体文件)
|
||||
COPY --from=builder /tmp/fonts.tar.gz /tmp/fonts.tar.gz
|
||||
RUN if [ -s /tmp/fonts.tar.gz ]; then \
|
||||
tar xzf /tmp/fonts.tar.gz -C ./internal/shared/pdf/fonts/ && \
|
||||
rm /tmp/fonts.tar.gz; \
|
||||
else \
|
||||
echo "字体文件不存在,将在运行时通过 volume 挂载或手动复制"; \
|
||||
rm /tmp/fonts.tar.gz; \
|
||||
fi
|
||||
|
||||
# 复制资源文件(从构建阶段复制)
|
||||
COPY --from=builder /app/resources ./resources
|
||||
|
||||
# 暴露端口
|
||||
EXPOSE 8080
|
||||
|
||||
@@ -3,7 +3,6 @@ package pdf
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/jung-kurt/gofpdf/v2"
|
||||
"go.uber.org/zap"
|
||||
@@ -16,23 +15,15 @@ type FontManager struct {
|
||||
chineseFontLoaded bool
|
||||
watermarkFontName string
|
||||
watermarkFontLoaded bool
|
||||
baseDir string // 缓存基础目录,避免重复计算
|
||||
}
|
||||
|
||||
// NewFontManager 创建字体管理器
|
||||
func NewFontManager(logger *zap.Logger) *FontManager {
|
||||
// 获取当前文件所在目录
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
baseDir := filepath.Dir(filename)
|
||||
|
||||
fm := &FontManager{
|
||||
return &FontManager{
|
||||
logger: logger,
|
||||
chineseFontName: "ChineseFont",
|
||||
watermarkFontName: "WatermarkFont",
|
||||
baseDir: baseDir,
|
||||
}
|
||||
|
||||
return fm
|
||||
}
|
||||
|
||||
// LoadChineseFont 加载中文字体到PDF
|
||||
@@ -133,77 +124,28 @@ func (fm *FontManager) getChineseFontPaths() []string {
|
||||
|
||||
// getWatermarkFontPaths 获取水印字体路径列表(仅TTF格式)
|
||||
func (fm *FontManager) getWatermarkFontPaths() []string {
|
||||
// 水印字体文件名(支持大小写变体)
|
||||
// 水印字体文件名(尝试大小写变体)
|
||||
fontNames := []string{
|
||||
"yunfengfeiyunti-2.ttf",
|
||||
"YunFengFeiYunTi-2.ttf", // 大写版本(兼容)
|
||||
"YunFengFeiYunTi-2.ttf", // 优先尝试大写版本
|
||||
"yunfengfeiyunti-2.ttf", // 小写版本(兼容)
|
||||
}
|
||||
|
||||
return fm.buildFontPaths(fontNames)
|
||||
}
|
||||
|
||||
// buildFontPaths 构建字体文件路径列表(支持多种路径查找方式)
|
||||
|
||||
// buildFontPaths 构建字体文件路径列表(仅从resources/pdf/fonts加载)
|
||||
func (fm *FontManager) buildFontPaths(fontNames []string) []string {
|
||||
var fontPaths []string
|
||||
|
||||
// 方式1: 服务器绝对路径(Linux环境,优先检查,因为服务器上文件通常在这里)
|
||||
if runtime.GOOS == "linux" {
|
||||
commonServerPaths := []string{
|
||||
"/www/tyapi-server/internal/shared/pdf/fonts",
|
||||
"/app/internal/shared/pdf/fonts",
|
||||
}
|
||||
for _, basePath := range commonServerPaths {
|
||||
for _, fontName := range fontNames {
|
||||
fontPaths = append(fontPaths, filepath.Join(basePath, fontName))
|
||||
}
|
||||
}
|
||||
}
|
||||
// 获取resources/pdf目录(使用统一的资源路径查找函数)
|
||||
resourcesPDFDir := GetResourcesPDFDir()
|
||||
fontsDir := filepath.Join(resourcesPDFDir, "fonts")
|
||||
|
||||
// 方式2: 使用 pdf/fonts/ 目录(相对于当前文件)
|
||||
// 只从resources/pdf/fonts目录加载
|
||||
for _, fontName := range fontNames {
|
||||
fontPaths = append(fontPaths, filepath.Join(fm.baseDir, "fonts", fontName))
|
||||
}
|
||||
|
||||
// 方式3: 尝试相对于工作目录的路径
|
||||
if workDir, err := os.Getwd(); err == nil {
|
||||
for _, fontName := range fontNames {
|
||||
paths := []string{
|
||||
filepath.Join(workDir, "internal", "shared", "pdf", "fonts", fontName),
|
||||
filepath.Join(workDir, "tyapi-server", "internal", "shared", "pdf", "fonts", fontName),
|
||||
}
|
||||
fontPaths = append(fontPaths, paths...)
|
||||
}
|
||||
}
|
||||
|
||||
// 方式4: 尝试使用可执行文件所在目录
|
||||
if execPath, err := os.Executable(); err == nil {
|
||||
execDir := filepath.Dir(execPath)
|
||||
if realPath, err := filepath.EvalSymlinks(execPath); err == nil {
|
||||
execDir = filepath.Dir(realPath)
|
||||
}
|
||||
for _, fontName := range fontNames {
|
||||
paths := []string{
|
||||
filepath.Join(execDir, "internal", "shared", "pdf", "fonts", fontName),
|
||||
filepath.Join(filepath.Dir(execDir), "internal", "shared", "pdf", "fonts", fontName),
|
||||
}
|
||||
fontPaths = append(fontPaths, paths...)
|
||||
}
|
||||
}
|
||||
|
||||
// 方式5: 尝试使用环境变量指定的路径
|
||||
if fontDir := os.Getenv("PDF_FONT_DIR"); fontDir != "" {
|
||||
for _, fontName := range fontNames {
|
||||
fontPaths = append(fontPaths, filepath.Join(fontDir, fontName))
|
||||
}
|
||||
}
|
||||
|
||||
// 方式6: 相对路径(作为最后的后备方案)
|
||||
for _, fontName := range fontNames {
|
||||
relativePaths := []string{
|
||||
"internal/shared/pdf/fonts/" + fontName,
|
||||
"./internal/shared/pdf/fonts/" + fontName,
|
||||
}
|
||||
fontPaths = append(fontPaths, relativePaths...)
|
||||
fontPath := filepath.Join(fontsDir, fontName)
|
||||
fontPaths = append(fontPaths, fontPath)
|
||||
}
|
||||
|
||||
// 过滤出实际存在的字体文件
|
||||
@@ -215,8 +157,6 @@ func (fm *FontManager) buildFontPaths(fontNames []string) []string {
|
||||
}
|
||||
}
|
||||
|
||||
// 字体文件不存在时不记录警告,使用系统默认字体即可
|
||||
|
||||
return existingFonts
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/jung-kurt/gofpdf/v2"
|
||||
@@ -51,106 +50,20 @@ func (g *PDFGenerator) registerChineseFont() string {
|
||||
return "ChineseFont"
|
||||
}
|
||||
|
||||
// getChineseFontPaths 获取中文字体路径(支持跨平台)
|
||||
func (g *PDFGenerator) getChineseFontPaths() []string {
|
||||
var fontPaths []string
|
||||
|
||||
// Windows系统
|
||||
if runtime.GOOS == "windows" {
|
||||
fontPaths = []string{
|
||||
`C:\Windows\Fonts\simhei.ttf`, // 黑体(优先,常用)
|
||||
`C:\Windows\Fonts\simsun.ttf`, // 宋体(如果存在单独的TTF文件)
|
||||
`C:\Windows\Fonts\msyh.ttf`, // 微软雅黑(如果存在单独的TTF文件)
|
||||
`C:\Windows\Fonts\simkai.ttf`, // 楷体
|
||||
}
|
||||
} else if runtime.GOOS == "linux" {
|
||||
// Linux系统常见字体路径
|
||||
fontPaths = []string{
|
||||
"/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",
|
||||
"/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc",
|
||||
"/usr/share/fonts/truetype/wqy/wqy-microhei.ttf",
|
||||
"/usr/share/fonts/truetype/wqy/wqy-zenhei.ttf",
|
||||
"/usr/share/fonts/truetype/arphic/uming.ttc",
|
||||
"/usr/share/fonts/truetype/arphic/ukai.ttc",
|
||||
"/usr/share/fonts/truetype/arphic/uming.ttf",
|
||||
"/usr/share/fonts/truetype/arphic/ukai.ttf",
|
||||
"/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf",
|
||||
"/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
|
||||
"/usr/share/fonts/truetype/noto/NotoSansCJK-Bold.ttc",
|
||||
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.otf",
|
||||
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
|
||||
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
|
||||
"/usr/local/share/fonts/simhei.ttf",
|
||||
"/usr/local/share/fonts/simsun.ttf",
|
||||
"/usr/local/share/fonts/simkai.ttf",
|
||||
"/opt/fonts/simhei.ttf",
|
||||
"/opt/fonts/simsun.ttf",
|
||||
"/opt/fonts/simkai.ttf",
|
||||
"/home/.fonts/simhei.ttf",
|
||||
"/home/.fonts/simsun.ttf",
|
||||
"/home/.fonts/simkai.ttf",
|
||||
"/root/.fonts/simhei.ttf",
|
||||
"/root/.fonts/simsun.ttf",
|
||||
"/root/.fonts/simkai.ttf",
|
||||
}
|
||||
} else if runtime.GOOS == "darwin" {
|
||||
// macOS系统字体路径
|
||||
fontPaths = []string{
|
||||
"/System/Library/Fonts/PingFang.ttc",
|
||||
"/System/Library/Fonts/STHeiti Light.ttc",
|
||||
"/System/Library/Fonts/STSong.ttc",
|
||||
"/Library/Fonts/Microsoft/SimHei.ttf",
|
||||
}
|
||||
}
|
||||
|
||||
// 过滤出实际存在的字体文件
|
||||
var existingFonts []string
|
||||
for _, fontPath := range fontPaths {
|
||||
if _, err := os.Stat(fontPath); err == nil {
|
||||
existingFonts = append(existingFonts, fontPath)
|
||||
}
|
||||
}
|
||||
|
||||
return existingFonts
|
||||
}
|
||||
|
||||
// findLogo 查找logo文件
|
||||
// findLogo 查找logo文件(仅从resources/pdf加载)
|
||||
func (g *PDFGenerator) findLogo() {
|
||||
// 获取当前文件所在目录
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
baseDir := filepath.Dir(filename)
|
||||
// 获取resources/pdf目录(使用统一的资源路径查找函数)
|
||||
resourcesPDFDir := GetResourcesPDFDir()
|
||||
logoPath := filepath.Join(resourcesPDFDir, "logo.png")
|
||||
|
||||
// 优先使用相对路径(Linux风格,使用正斜杠)
|
||||
logoPaths := []string{
|
||||
"internal/shared/pdf/天远数据.png", // 相对于项目根目录(最常用)
|
||||
"./internal/shared/pdf/天远数据.png", // 当前目录下的相对路径
|
||||
filepath.Join(baseDir, "天远数据.png"), // 相对当前文件
|
||||
}
|
||||
|
||||
// 尝试相对路径
|
||||
for _, logoPath := range logoPaths {
|
||||
if _, err := os.Stat(logoPath); err == nil {
|
||||
g.logoPath = logoPath
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试服务器绝对路径(后备方案)
|
||||
if runtime.GOOS == "linux" {
|
||||
serverPaths := []string{
|
||||
"/www/tyapi-server/internal/shared/pdf/天远数据.png",
|
||||
"/app/internal/shared/pdf/天远数据.png",
|
||||
}
|
||||
for _, logoPath := range serverPaths {
|
||||
if _, err := os.Stat(logoPath); err == nil {
|
||||
g.logoPath = logoPath
|
||||
return
|
||||
}
|
||||
}
|
||||
// 检查文件是否存在
|
||||
if _, err := os.Stat(logoPath); err == nil {
|
||||
g.logoPath = logoPath
|
||||
return
|
||||
}
|
||||
|
||||
// 只记录关键错误
|
||||
g.logger.Warn("未找到logo文件")
|
||||
g.logger.Warn("未找到logo文件", zap.String("path", logoPath))
|
||||
}
|
||||
|
||||
// GenerateProductPDF 为产品生成PDF文档(接受响应类型,内部转换)
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"tyapi-server/internal/domains/product/entities"
|
||||
|
||||
@@ -55,50 +54,20 @@ func NewPDFGeneratorRefactored(logger *zap.Logger) *PDFGeneratorRefactored {
|
||||
return gen
|
||||
}
|
||||
|
||||
// findLogo 查找logo文件
|
||||
// findLogo 查找logo文件(仅从resources/pdf加载)
|
||||
func (g *PDFGeneratorRefactored) findLogo() {
|
||||
// 获取当前文件所在目录
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
baseDir := filepath.Dir(filename)
|
||||
// 获取resources/pdf目录(使用统一的资源路径查找函数)
|
||||
resourcesPDFDir := GetResourcesPDFDir()
|
||||
logoPath := filepath.Join(resourcesPDFDir, "logo.png")
|
||||
|
||||
// 优先使用 baseDir(最可靠,基于当前文件位置)
|
||||
logoPaths := []string{
|
||||
filepath.Join(baseDir, "天远数据.png"), // 相对当前文件(最优先)
|
||||
}
|
||||
|
||||
// 尝试相对于工作目录的路径
|
||||
if workDir, err := os.Getwd(); err == nil {
|
||||
logoPaths = append(logoPaths,
|
||||
filepath.Join(workDir, "internal", "shared", "pdf", "天远数据.png"),
|
||||
filepath.Join(workDir, "tyapi-server", "internal", "shared", "pdf", "天远数据.png"),
|
||||
)
|
||||
}
|
||||
|
||||
// 尝试服务器绝对路径(Linux环境,优先查找 /www/tyapi-server)
|
||||
if runtime.GOOS == "linux" {
|
||||
serverPaths := []string{
|
||||
"/www/tyapi-server/internal/shared/pdf/天远数据.png",
|
||||
"/app/internal/shared/pdf/天远数据.png",
|
||||
}
|
||||
logoPaths = append(logoPaths, serverPaths...)
|
||||
}
|
||||
|
||||
// 尝试相对路径(作为最后的后备方案)
|
||||
logoPaths = append(logoPaths,
|
||||
"internal/shared/pdf/天远数据.png",
|
||||
"./internal/shared/pdf/天远数据.png",
|
||||
)
|
||||
|
||||
// 尝试所有路径
|
||||
for _, logoPath := range logoPaths {
|
||||
if _, err := os.Stat(logoPath); err == nil {
|
||||
g.logoPath = logoPath
|
||||
return
|
||||
}
|
||||
// 检查文件是否存在
|
||||
if _, err := os.Stat(logoPath); err == nil {
|
||||
g.logoPath = logoPath
|
||||
return
|
||||
}
|
||||
|
||||
// 只记录关键错误
|
||||
g.logger.Warn("未找到logo文件")
|
||||
g.logger.Warn("未找到logo文件", zap.String("path", logoPath))
|
||||
}
|
||||
|
||||
// GenerateProductPDF 为产品生成PDF文档(接受响应类型,内部转换)
|
||||
|
||||
53
internal/shared/pdf/resources_path.go
Normal file
53
internal/shared/pdf/resources_path.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package pdf
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// GetResourcesPDFDir 获取resources/pdf目录路径
|
||||
// 支持开发和生产环境:
|
||||
// - 开发环境:从工作目录或可执行文件目录查找
|
||||
// - 生产环境:从可执行文件目录查找(Docker容器中工作目录为/app)
|
||||
func GetResourcesPDFDir() string {
|
||||
// 优先级1: 从工作目录查找(适用于开发环境和生产环境)
|
||||
if workDir, err := os.Getwd(); err == nil {
|
||||
// 检查当前工作目录下的resources/pdf
|
||||
resourcesPath := filepath.Join(workDir, "resources", "pdf")
|
||||
if _, err := os.Stat(resourcesPath); err == nil {
|
||||
return resourcesPath
|
||||
}
|
||||
// 检查tyapi-server-gin子目录(开发环境)
|
||||
resourcesPath = filepath.Join(workDir, "tyapi-server-gin", "resources", "pdf")
|
||||
if _, err := os.Stat(resourcesPath); err == nil {
|
||||
return resourcesPath
|
||||
}
|
||||
}
|
||||
|
||||
// 优先级2: 从可执行文件所在目录查找(适用于生产环境Docker容器)
|
||||
// 在生产环境中,可执行文件在/app/tyapi-server,资源在/app/resources
|
||||
if execPath, err := os.Executable(); err == nil {
|
||||
execDir := filepath.Dir(execPath)
|
||||
// 处理符号链接
|
||||
if realPath, err := filepath.EvalSymlinks(execPath); err == nil {
|
||||
execDir = filepath.Dir(realPath)
|
||||
}
|
||||
|
||||
// 检查可执行文件同目录下的resources/pdf
|
||||
resourcesPath := filepath.Join(execDir, "resources", "pdf")
|
||||
if _, err := os.Stat(resourcesPath); err == nil {
|
||||
return resourcesPath
|
||||
}
|
||||
|
||||
// 检查可执行文件父目录下的resources/pdf
|
||||
// 适用于生产环境:/app/tyapi-server -> /app/resources
|
||||
resourcesPath = filepath.Join(filepath.Dir(execDir), "resources", "pdf")
|
||||
if _, err := os.Stat(resourcesPath); err == nil {
|
||||
return resourcesPath
|
||||
}
|
||||
}
|
||||
|
||||
// 优先级3: 返回相对路径作为后备(相对于当前工作目录)
|
||||
return filepath.Join("resources", "pdf")
|
||||
}
|
||||
|
||||
BIN
resources/pdf/fonts/YunFengFeiYunTi-2.ttf
Normal file
BIN
resources/pdf/fonts/YunFengFeiYunTi-2.ttf
Normal file
Binary file not shown.
BIN
resources/pdf/fonts/simfang.ttf
Normal file
BIN
resources/pdf/fonts/simfang.ttf
Normal file
Binary file not shown.
BIN
resources/pdf/fonts/simhei.ttf
Normal file
BIN
resources/pdf/fonts/simhei.ttf
Normal file
Binary file not shown.
BIN
resources/pdf/fonts/simkai.ttf
Normal file
BIN
resources/pdf/fonts/simkai.ttf
Normal file
Binary file not shown.
|
Before Width: | Height: | Size: 142 KiB After Width: | Height: | Size: 142 KiB |
Reference in New Issue
Block a user