From e9fa67ae0062ce9ad7de3e7913c763c4eee2f438 Mon Sep 17 00:00:00 2001 From: cjy Date: Wed, 21 Jan 2026 16:09:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dpdf=E6=B8=B2=E6=9F=93?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=8F=AA=E8=83=BD=E6=B8=B2=E6=9F=93jpg?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/utils/pdf.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/utils/pdf.go b/pkg/utils/pdf.go index aac6d39..a89ff51 100644 --- a/pkg/utils/pdf.go +++ b/pkg/utils/pdf.go @@ -5,7 +5,9 @@ import ( "fmt" "io" "net/http" + "net/url" "os" + "path/filepath" "unicode" "github.com/phpdave11/gofpdf" @@ -81,6 +83,17 @@ func GeneratePDF(text, imageURL, outputPath, fontPath string) error { // 添加一些间距 pdf.Ln(5) + // 解析URL获取文件扩展名 + u, err := url.Parse(imageURL) + if err != nil { + return fmt.Errorf("图片链接解析错误: %v", err) + } + fileExt := filepath.Ext(u.Path) + // 如果没有扩展名,默认使用.jpg + if fileExt == "" { + fileExt = ".jpg" + } + // 下载图片 resp, err := http.Get(imageURL) if err != nil { @@ -95,7 +108,7 @@ func GeneratePDF(text, imageURL, outputPath, fontPath string) error { } // 将图片数据保存到临时文件(gofpdf需要文件路径) - tmpFile, err := os.CreateTemp("", "pdf_image_*.jpg") + tmpFile, err := os.CreateTemp("", "pdf_image_*"+fileExt) if err != nil { return fmt.Errorf("创建临时文件失败: %v", err) }