- chromedp 通道新增源文件入口,file:// 加载原始HTML - 相对资源在原目录天然解析,printBackground 保背景色 - 预览工具栏新增导出按钮,默认文件名取当前HTML名 - 远程文件复用站点缓存路径,本地远程同一命令 - 浏览器探测扩Chrome+Edge双查,Markdown通道重复探测归一
89 lines
1.9 KiB
Go
89 lines
1.9 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
|
|
}
|
|
|
|
// SelectPDFSaveDirectory 选择PDF保存目录
|
|
func (a *App) SelectPDFSaveDirectory() (string, error) {
|
|
pdfAPI, err := a.requirePdfAPI()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return pdfAPI.SelectDirectory()
|
|
}
|