PDF 导出 — Aspose.Note FOSS for Python

Aspose.Note FOSS for Python supports exporting loaded .one 文档转换为 PDF via Document.Save(). PDF 渲染由可选的 ReportLab 库。这是目前唯一实现的保存格式;其他 SaveFormat 值会引发 UnsupportedSaveFormatException.


先决条件

使用以下方式安装库 [pdf] 额外的依赖来引入 ReportLab::

pip install "aspose-note[pdf]"

验证::

python -c "from aspose.note import Document, SaveFormat; print('PDF export ready')"

基本导出

将文档的所有页面导出为单个 PDF 文件::

from aspose.note import Document, SaveFormat

doc = Document("MyNotes.one")
doc.Save("output.pdf", SaveFormat.Pdf)

页面在 PDF 中的顺序与它们在 DOM 中出现的顺序相同。.


使用 PdfSaveOptions

PdfSaveOptions 提供对导出的细粒度控制。传入它而不是裸 SaveFormat 枚举::

from aspose.note import Document
from aspose.note.saving import PdfSaveOptions

opts = PdfSaveOptions()

doc = Document("MyNotes.one")
doc.Save("output.pdf", opts)

关于 PageIndex / PageCount::这些字段存在于 PdfSaveOptions未在 v26.3.1 中转发给 PDF 导出器 且没有任何影响。整个文档始终会被导出。.


PdfSaveOptions 参考

属性类型默认描述
PageIndexint0字段存在但 未在 v26.3.1 中转发到 PDF 导出器 (无效)
PageCount`intNone`None

批量导出

转换每个 .one 目录中的文件为 PDF::

from pathlib import Path
from aspose.note import Document, SaveFormat

input_dir = Path("./notes")
output_dir = Path("./pdfs")
output_dir.mkdir(parents=True, exist_ok=True)

for one_file in sorted(input_dir.glob("*.one")):
    try:
        doc = Document(str(one_file))
        out = output_dir / one_file.with_suffix(".pdf").name
        doc.Save(str(out), SaveFormat.Pdf)
        print(f"OK  {one_file.name} -> {out.name}")
    except Exception as exc:
        print(f"ERR {one_file.name}: {exc}")

从流加载,保存到文件

将基于流的加载与基于文件的 PDF 输出相结合::

from pathlib import Path
from aspose.note import Document, SaveFormat

one_bytes = Path("MyNotes.one").read_bytes()

import io
doc = Document(io.BytesIO(one_bytes))
doc.Save("output.pdf", SaveFormat.Pdf)

在内存中获取 PDF 字节

Document.Save() 直接接受二进制流:无需临时文件::

import io
from aspose.note import Document, SaveFormat
from aspose.note.saving import PdfSaveOptions

doc = Document("MyNotes.one")

buf = io.BytesIO()
doc.Save(buf, PdfSaveOptions())
pdf_bytes = buf.getvalue()
print(f"PDF size: {len(pdf_bytes)} bytes")

支持的 SaveFormat 值

SaveFormat状态
SaveFormat.Pdf已实现

常见错误

错误原因修复
ImportError: No module named 'reportlab'[pdf] extra 未安装pip install "aspose-note[pdf]"
UnsupportedSaveFormatException使用了非 PDF SaveFormat使用 SaveFormat.Pdf
IncorrectPasswordException已加密的 .one 文件使用未加密的文件
FileNotFoundError输入 .one 路径错误使用以下方式验证路径 Path.exists()
输出时权限错误输出目录不可写检查输出目录权限
 中文