- 浏览器探测补按用户安装路径LOCALAPPDATA - fs-agent远程HTML导出先拉取到本地临时目录 - Markdown通道空结果守卫,杜绝0字节PDF误报成功 - 清理死参数与无调用死代码
79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"u-desk/internal/api"
|
|
)
|
|
|
|
// requirePdfAPI 检查 pdfAPI 是否已初始化
|
|
func (a *App) requirePdfAPI() (*api.PdfAPI, error) {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
if a.pdfAPI == nil {
|
|
return nil, fmt.Errorf("PDF导出功能正在初始化中")
|
|
}
|
|
return a.pdfAPI, nil
|
|
}
|
|
|
|
// ExportPDF 导出PDF文件
|
|
func (a *App) ExportPDF(content string, title string, fileName string, fontSize int, pageWidth int, pageHeight int) (map[string]interface{}, error) {
|
|
pdfAPI, err := a.requirePdfAPI()
|
|
if err != nil {
|
|
return map[string]interface{}{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
}, err
|
|
}
|
|
|
|
req := api.PdfExportRequest{
|
|
Content: content,
|
|
Title: title,
|
|
FileName: fileName,
|
|
FontSize: fontSize,
|
|
PageWidth: pageWidth,
|
|
PageHeight: pageHeight,
|
|
}
|
|
|
|
result, err := pdfAPI.ExportMarkdownToPDF(req)
|
|
if err != nil {
|
|
return map[string]interface{}{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
}, err
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"success": result.Success,
|
|
"message": result.Message,
|
|
"path": result.Path,
|
|
"size": result.Size,
|
|
}, nil
|
|
}
|
|
|
|
// ExportHtmlFileToPDF 将HTML文件导出为PDF
|
|
func (a *App) ExportHtmlFileToPDF(path string, saveName string) (map[string]interface{}, error) {
|
|
pdfAPI, err := a.requirePdfAPI()
|
|
if err != nil {
|
|
return map[string]interface{}{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
}, err
|
|
}
|
|
|
|
result, err := pdfAPI.ExportHtmlFileToPDF(path, saveName)
|
|
if err != nil {
|
|
return map[string]interface{}{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
}, err
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"success": result.Success,
|
|
"message": result.Message,
|
|
"path": result.Path,
|
|
"size": result.Size,
|
|
}, nil
|
|
}
|