Documentation
¶
Overview ¶
Package converter 提供 OFD 文档到 PDF、文本和图像等格式的转换能力。
Index ¶
- Variables
- func Image(input interface{}, opts ...Option) error
- func ImageDocument(doc *render.Document, opts ...Option) error
- func ImageDocuments(documents []*render.Document, opts ...Option) error
- func PDF(input interface{}, output io.Writer, opts ...Option) error
- func PDFDocuments(documents []*render.Document, output io.Writer, opts ...Option) error
- func Text(input interface{}, output io.Writer, opts ...Option) error
- func TextDocument(doc *parser.Document, output io.Writer, opts ...Option) error
- func TextDocuments(documents []*parser.Document, output io.Writer, opts ...Option) error
- type Converter
- type Option
- func BgColor(bg color.Color) Option
- func DPI(dpi float64) Option
- func EPS() Option
- func ImageWriter(f func(page int, img image.Image) error) Option
- func JPG() Option
- func PNG() Option
- func Page(page int) Option
- func SVG() Option
- func TeX() Option
- func Thumbnail(s int) Option
- func Writer(f func(page int) (io.WriteCloser, error)) Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrInvalidPage = errors.New("页码无效")
ErrInvalidPage 表示请求的全局页码无效。
Functions ¶
func ImageDocument ¶ added in v0.0.6
ImageDocument 将已解析的 OFD 文档渲染为图像或矢量格式。
func ImageDocuments ¶ added in v0.0.6
ImageDocuments 将多个已解析的 OFD 文档体按全局页码渲染为图像或矢量格式。
func PDF ¶
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
PDFDocuments 将多个已解析的 OFD 文档体按全局页码写入同一个 PDF。
func TextDocument ¶ added in v0.0.6
TextDocument 提取已解析 OFD 文档中的文字并写入 output。
Types ¶
type Converter ¶
type Converter struct {
// contains filtered or unexported fields
}
Converter 配置转换器
type Option ¶
type Option func(*Converter)
Option 配置选项类型
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 ¶
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 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
Click to show internal directories.
Click to hide internal directories.