converter

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2026 License: Unlicense Imports: 15 Imported by: 0

Documentation

Overview

Package converter 提供 OFD 文档到 PDF、文本和图像等格式的转换能力。

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidPage = errors.New("页码无效")

ErrInvalidPage 表示请求的全局页码无效。

Functions

func Image

func Image(input interface{}, opts ...Option) error

Image 渲染OFD文档

func ImageDocument added in v0.0.6

func ImageDocument(doc *render.Document, opts ...Option) error

ImageDocument 将已解析的 OFD 文档渲染为图像或矢量格式。

func ImageDocuments added in v0.0.6

func ImageDocuments(documents []*render.Document, opts ...Option) error

ImageDocuments 将多个已解析的 OFD 文档体按全局页码渲染为图像或矢量格式。

func PDF

func PDF(input interface{}, output io.Writer, opts ...Option) error
Example
package main

import (
	"fmt"
	"os"

	"github.com/zc310/ofd/pkg/converter"
)

func main() {
	output, err := os.CreateTemp("", "ofd-example-*.pdf")
	if err == nil {
		defer os.Remove(output.Name())
		err = converter.PDF("../../test/testdata/intro.ofd", output)
		if closeErr := output.Close(); err == nil {
			err = closeErr
		}
	}
	fmt.Println(err == nil)
}
Output:
true

func PDFDocuments added in v0.0.6

func PDFDocuments(documents []*render.Document, output io.Writer, opts ...Option) error

PDFDocuments 将多个已解析的 OFD 文档体按全局页码写入同一个 PDF。

func Text added in v0.0.6

func Text(input interface{}, output io.Writer, opts ...Option) error

Text 提取 OFD 文档中的文字并写入 output。 不会保留字体、颜色和布局信息;不同文字对象按行输出,不同页面使用分页符分隔。

func TextDocument added in v0.0.6

func TextDocument(doc *parser.Document, output io.Writer, opts ...Option) error

TextDocument 提取已解析 OFD 文档中的文字并写入 output。

func TextDocuments added in v0.0.6

func TextDocuments(documents []*parser.Document, output io.Writer, opts ...Option) error

TextDocuments 按全局页码提取多个已解析 OFD 文档体中的文字并写入 output。

Types

type Converter

type Converter struct {
	// contains filtered or unexported fields
}

Converter 配置转换器

type Option

type Option func(*Converter)

Option 配置选项类型

func BgColor

func BgColor(bg color.Color) Option

BgColor 设置背景颜色

func DPI

func DPI(dpi float64) Option

DPI 设置DPI

func EPS added in v0.0.6

func EPS() Option

EPS 设置为 Encapsulated PostScript 格式

Example
package main

import (
	"bytes"
	"fmt"
	"io"

	"github.com/zc310/ofd/pkg/converter"
)

type exampleBufferWriteCloser struct {
	*bytes.Buffer
}

func (exampleBufferWriteCloser) Close() error { return nil }

func main() {
	var output bytes.Buffer
	err := converter.Image("../../test/testdata/helloworld.ofd",
		converter.Writer(func(int) (io.WriteCloser, error) {
			return exampleBufferWriteCloser{Buffer: &output}, nil
		}),
		converter.EPS(),
		converter.Page(1),
	)
	fmt.Println(err == nil && bytes.Contains(output.Bytes(), []byte("%!PS-Adobe-3.0 EPSF-3.0")))
}
Output:
true

func ImageWriter

func ImageWriter(f func(page int, img image.Image) error) Option

ImageWriter 设置图像写入器

func JPG

func JPG() Option

JPG 设置为JPEG格式

Example
package main

import (
	"fmt"
	"image/color"
	"io"
	"os"
	"path/filepath"

	"github.com/zc310/ofd/pkg/converter"
)

func main() {
	dir, err := os.MkdirTemp("", "ofd-example-")
	if err == nil {
		defer os.RemoveAll(dir)
		err = converter.Image("../../test/testdata/intro.ofd",
			converter.Writer(func(page int) (io.WriteCloser, error) {
				return os.Create(filepath.Join(dir, fmt.Sprintf("intro_%d.jpg", page)))
			}),
			converter.BgColor(color.White),
			converter.JPG(),
			converter.Page(40),
			converter.DPI(300),
		)
	}
	fmt.Println(err == nil)
}
Output:
true

func PNG

func PNG() Option

PNG 设置为PNG格式

Example
package main

import (
	"fmt"
	"image/color"
	"io"
	"os"
	"path/filepath"

	"github.com/zc310/ofd/pkg/converter"
)

func main() {
	dir, err := os.MkdirTemp("", "ofd-example-")
	if err == nil {
		defer os.RemoveAll(dir)
		err = converter.Image("../../test/testdata/ano.ofd",
			converter.Writer(func(page int) (io.WriteCloser, error) {
				return os.Create(filepath.Join(dir, fmt.Sprintf("ano_%d.png", page)))
			}),
			converter.BgColor(color.White),
			converter.PNG(),
		)
	}
	fmt.Println(err == nil)
}
Output:
true

func Page

func Page(page int) Option

Page 设置全局页码,从 1 开始;0 表示处理全部页面。

func SVG added in v0.0.6

func SVG() Option

SVG 设置为 SVG 格式

Example
package main

import (
	"bytes"
	"fmt"
	"io"

	"github.com/zc310/ofd/pkg/converter"
)

type exampleBufferWriteCloser struct {
	*bytes.Buffer
}

func (exampleBufferWriteCloser) Close() error { return nil }

func main() {
	var output bytes.Buffer
	err := converter.Image("../../test/testdata/helloworld.ofd",
		converter.Writer(func(int) (io.WriteCloser, error) {
			return exampleBufferWriteCloser{Buffer: &output}, nil
		}),
		converter.SVG(),
		converter.Page(1),
	)
	fmt.Println(err == nil && bytes.Contains(output.Bytes(), []byte("<svg")))
}
Output:
true

func TeX added in v0.0.6

func TeX() Option

TeX 设置为 TeX/PGF 格式

Example
package main

import (
	"bytes"
	"fmt"
	"io"

	"github.com/zc310/ofd/pkg/converter"
)

type exampleBufferWriteCloser struct {
	*bytes.Buffer
}

func (exampleBufferWriteCloser) Close() error { return nil }

func main() {
	var output bytes.Buffer
	err := converter.Image("../../test/testdata/helloworld.ofd",
		converter.Writer(func(int) (io.WriteCloser, error) {
			return exampleBufferWriteCloser{Buffer: &output}, nil
		}),
		converter.TeX(),
		converter.Page(1),
	)
	fmt.Println(err == nil && bytes.Contains(output.Bytes(), []byte("\\begin{pgfpicture}")))
}
Output:
true

func Thumbnail

func Thumbnail(s int) Option

Thumbnail 设置缩略图大小

func Writer

func Writer(f func(page int) (io.WriteCloser, error)) Option

Writer 设置文件写入器

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL