Documentation
¶
Overview ¶
Package pdfcpuwriter 提供实现 canvas.Renderer 的实验性多页 PDF 写入器。
本包不是完整 PDF 编码器。使用 pdfcpu 构建标签时,canvas 后端将其注册为 PDF 写入器。 pdfcpu 负责对象、流和交叉引用写出; 几何、整形文字和字体子集仍使用 OFD 内置 canvas 及已有 font 依赖。 不支持的绘制能力通过 Err 或 Write 返回 ErrUnsupported,不静默降级。 不支持并发调用;文档和字体资源必须保持有效,直到 Write 返回。
Example ¶
package main
import (
"bytes"
"context"
"fmt"
"github.com/zc310/ofd/internal/pdfcpuwriter"
"github.com/zc310/ofd/internal/thirdparty/canvas"
)
func main() {
doc, err := pdfcpuwriter.New(context.Background(), 210, 297)
if err != nil {
fmt.Println(err)
return
}
page := canvas.NewContext(doc)
page.SetFillColor(canvas.Black)
page.DrawPath(10, 20, canvas.Rectangle(30, 15))
if err := doc.NextPage(297, 210); err != nil {
fmt.Println(err)
return
}
page = canvas.NewContext(doc)
page.DrawPath(20, 30, canvas.Rectangle(40, 10))
var output bytes.Buffer
if err := doc.Write(&output); err != nil {
fmt.Println(err)
return
}
fmt.Println(doc.Stats().Pages, bytes.HasPrefix(output.Bytes(), []byte("%PDF-")))
}
Output: 2 true
Index ¶
- Variables
- type Document
- func (r *Document) AddLinks(links []drawing.PageLink) error
- func (r *Document) AddOutlines(outlines []drawing.PageOutline) error
- func (r *Document) Err() error
- func (r *Document) NextPage(width, height float64) error
- func (r *Document) RenderClippedImage(img image.Image, matrix canvas.Matrix, clip *canvas.Path)
- func (r *Document) RenderClippedPath(path *canvas.Path, style canvas.Style, matrix canvas.Matrix, clip *canvas.Path)
- func (r *Document) RenderClippedText(text *canvas.Text, matrix canvas.Matrix, clip *canvas.Path)
- func (r *Document) RenderImage(img image.Image, matrix canvas.Matrix)
- func (r *Document) RenderPath(path *canvas.Path, style canvas.Style, matrix canvas.Matrix)
- func (r *Document) RenderText(text *canvas.Text, matrix canvas.Matrix)
- func (r *Document) RenderTextWithPaint(text *canvas.Text, matrix canvas.Matrix, clip *canvas.Path, paint canvas.Paint, ...) bool
- func (r *Document) Size() (float64, float64)
- func (r *Document) Stats() Stats
- func (r *Document) Write(w io.Writer) error
- type Options
- type Stats
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("pdfcpu实验写入器已结束")
ErrClosed 表示已经尝试写出,不能继续绘制、换页或再次写出。
var ErrUnsupported = errors.New("pdfcpu实验写入器尚不支持此功能")
ErrUnsupported 表示尚未支持的绘制能力,可通过 errors.Is 识别。
Functions ¶
This section is empty.
Types ¶
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document 是基于 pdfcpu 的实验性 PDF 写入器,同时实现 canvas.Renderer。 不支持并发调用;可通过 pdfcpu 构建标签启用实验后端;坐标单位为毫米,y 轴向上。 Content operators follow ISO 32000-1 sections 8.4/8.5/8.9 and 9.4/9.7/9.10.
func NewWithOptions ¶
NewWithOptions 创建含第一页的写入器,并指定图片编码选项。
Example ¶
package main
import (
"bytes"
"context"
"fmt"
"github.com/zc310/ofd/internal/pdfcpuwriter"
"github.com/zc310/ofd/internal/render/drawing"
)
func main() {
doc, err := pdfcpuwriter.NewWithOptions(context.Background(), 210, 297, pdfcpuwriter.Options{LossyImages: true})
if err != nil {
fmt.Println(err)
return
}
if err := doc.AddLinks([]drawing.PageLink{{URI: "https://example.com", X: 10, Y: 10, Width: 30, Height: 10}}); err != nil {
fmt.Println(err)
return
}
if err := doc.AddOutlines([]drawing.PageOutline{{Title: "首页", Target: &drawing.LinkTarget{Page: 0, Type: drawing.DestFit}}}); err != nil {
fmt.Println(err)
return
}
var output bytes.Buffer
if err := doc.Write(&output); err != nil {
fmt.Println(err)
return
}
fmt.Println(bytes.HasPrefix(output.Bytes(), []byte("%PDF-")))
}
Output: true
func (*Document) AddLinks ¶
AddLinks 给当前页添加链接;目标页可以尚未写入,在 Write 时统一解析。 链接矩形及目标沿用 drawing 的毫米、左上角坐标约定。
func (*Document) AddOutlines ¶
func (r *Document) AddOutlines(outlines []drawing.PageOutline) error
AddOutlines 按先序顺序追加大纲,Level 表示层级;内部目标与 URI 二选一。
func (*Document) RenderClippedImage ¶
RenderClippedImage 在图像局部坐标定义的裁剪路径内绘制图片。
func (*Document) RenderClippedPath ¶
func (r *Document) RenderClippedPath(path *canvas.Path, style canvas.Style, matrix canvas.Matrix, clip *canvas.Path)
RenderClippedPath 在局部裁剪路径内绘制,clip 使用图元局部坐标。
func (*Document) RenderClippedText ¶
RenderClippedText 在文字局部坐标定义的裁剪路径内绘制文字。
func (*Document) RenderImage ¶
RenderImage 绘制图片,失败通过 Err 返回。
func (*Document) RenderPath ¶
RenderPath 绘制路径,失败通过 Err 返回。
func (*Document) RenderText ¶
RenderText 绘制已整形的文字,保留字形及源文本语义。
func (*Document) RenderTextWithPaint ¶
func (r *Document) RenderTextWithPaint(text *canvas.Text, matrix canvas.Matrix, clip *canvas.Path, paint canvas.Paint, paintMatrix canvas.Matrix) bool
RenderTextWithPaint 使用独立画笔坐标绘制渐变文字。 不支持的画笔返回 false,供调用方执行现有回退;其他绘制失败通过 Err 返回。