feat(文档导出): 支持指定页导出,统一文件编号
build.yaml / build (push) Successful in 39s

This commit is contained in:
xiaoqidun committed 2026-09-14 12:11:53 +08:00
1 parent d3904521b9
commit 3e87048a30
8 files changed
+336 -32

No files matched your search

+26 -2
View File
@@ -21,6 +21,8 @@ import (
"encoding/json"
"fmt"
"syscall/js"
"github.com/xiaoqidun/ofdgo"
)
// exportWriter 分块传递导出数据
@@ -74,6 +76,7 @@ func RunWASM() {
registerCallback("ofdgoSVGFontData", svgFontData)
registerCallback("ofdgoSearchPage", searchPage)
registerCallback("ofdgoExportFormats", exportFormats)
registerCallback("ofdgoParsePageRange", parsePageRange)
registerExportCallback("ofdgoExportPage", exportPage)
registerExportCallback("ofdgoExportDocument", exportDocument)
registerCallback("ofdgoMatchFontFiles", matchFontFiles)
@@ -287,6 +290,20 @@ func exportFormats(args []js.Value) (any, error) {
return ExportFormats(), nil
}
// parsePageRange 解析当前文档的导出页码
// 入参: args 浏览器参数
// 返回: any 页面索引, error 错误信息
func parsePageRange(args []js.Value) (any, error) {
if currentSession == nil {
return nil, fmt.Errorf("ofd document is not opened")
}
count, err := currentSession.Reader.PageCount()
if err != nil {
return nil, err
}
return ofdgo.ParsePageRange(args[0].String(), count)
}
// exportPage 导出OFD单页
// 入参: args 浏览器参数
// 返回: any 导出结果, error 错误信息
@@ -318,8 +335,15 @@ func exportDocument(args []js.Value) (any, error) {
if currentSession == nil {
return nil, fmt.Errorf("ofd document is not opened")
}
writer := bufio.NewWriterSize(exportWriter{write: args[2]}, 1<<20)
format, err := currentSession.ExportDocument(args[0].String(), args[1].Float(), writer)
var indices []int
if !args[2].IsNull() {
indices = make([]int, args[2].Length())
for i := range indices {
indices[i] = args[2].Index(i).Int()
}
}
writer := bufio.NewWriterSize(exportWriter{write: args[3]}, 1<<20)
format, err := currentSession.ExportDocument(args[0].String(), args[1].Float(), writer, indices...)
if err != nil {
return nil, err
}
+9 -6
View File
@@ -614,15 +614,15 @@ func signatureStampInfos(positions []ofdgo.SignatureStampPosition) []SignatureSt
}
// ExportDocument 导出文档为PDF或逐页打包ZIP
// 入参: value 导出格式, dpi 图片DPI, writer 输出流
// 入参: value 导出格式, dpi 图片DPI, writer 输出流, indices 零基页面索引,省略则全部
// 返回: ExportFormat 导出格式, error 错误信息
func (s *Session) ExportDocument(value string, dpi float64, writer io.Writer) (ExportFormat, error) {
func (s *Session) ExportDocument(value string, dpi float64, writer io.Writer, indices ...int) (ExportFormat, error) {
format, ok := exportFormat(value)
if !ok {
return ExportFormat{}, fmt.Errorf("unsupported export format %s", value)
}
if format.Value == "pdf" {
return format, s.ExportPDF(writer)
return format, s.ExportPDF(writer, indices...)
}
if s == nil || s.Reader == nil || s.Renderer == nil || s.doc == nil {
return ExportFormat{}, fmt.Errorf("ofd document is not opened")
@@ -631,17 +631,20 @@ func (s *Session) ExportDocument(value string, dpi float64, writer io.Writer) (E
if dpi > 0 && (format.Value == "png" || format.Value == "jpg") {
renderer.DPI = dpi
}
err := renderer.RenderToZIP(writer, format.Value)
err := renderer.RenderToZIP(writer, format.Value, indices...)
return ExportFormat{Value: "zip", Label: "ZIP", Extension: "zip", MIME: "application/zip"}, err
}
// ExportPDF 导出文档为PDF
// 入参: writer 输出流
// 入参: writer 输出流, indices 零基页面索引,省略则全部
// 返回: error 错误信息
func (s *Session) ExportPDF(writer io.Writer) error {
func (s *Session) ExportPDF(writer io.Writer, indices ...int) error {
if s == nil || s.Reader == nil || s.Renderer == nil || s.doc == nil {
return fmt.Errorf("ofd document is not opened")
}
if len(indices) > 0 {
return s.Renderer.RenderToMultiPagePDF(writer, indices...)
}
pages := make([]*ofdgo.PageContent, len(s.doc.Pages.Page))
for i := range pages {
page, err := s.pageContent(i)