我们很高兴宣布 GroupDocs.Viewer for Python v26.11 已于 2026年11月 发布。此版本将底层 GroupDocs.Viewer 引擎升级至 .NET 10 构建的 26.10 版,并为 Python API 引入了重要改进——包括能够取消长时间运行的渲染、渲染到内存流,以及使用新的 groupdocs-viewer CLI 命令在浏览器中直接打开文档。它还解决了与内存泄漏、进程分叉和文件处理相关的关键稳定性问题。
本次发布的新功能
- [Feature] 添加
CadOptions.enable_layered_rendering以在 HTML 输出中显示和隐藏 CAD 图层,而无需重新渲染 (VIEWERPYTHON-180) - [Feature] 添加
PdfOptions.fix_thai_marks以在将 PDF 渲染为 HTML 时修复孤立的泰文标记 (VIEWERPYTHON-181) - [Feature] 使用
CancellationTokenSource和view(options, cancellation_token=...)取消正在运行的渲染 (VIEWERPYTHON-182) - [Feature] 添加
groupdocs-viewer命令以在浏览器中打开文档或文件夹 (VIEWERPYTHON-199) - [Enhancement] 将 GroupDocs.Viewer 引擎从 26.8 更新至 26.10,使用其 .NET 10 构建 (VIEWERPYTHON-183)
- [Enhancement] 当
GROUPDOCS_LIC_PATH中的许可证无法应用时抛出LicenseError,而不是静默运行评估模式 (VIEWERPYTHON-184) - [Enhancement] 通过指针而非 base64 JSON 将流输入和
BytesIO输出传递给 .NET (VIEWERPYTHON-185) - [Enhancement] 从
groupdocs-shared-net包安装所有 GroupDocs Python 包共享的模块 (VIEWERPYTHON-186) - [Enhancement] 将 docstring 链接到 Python 文档,并为每个重载提供调用示例 (VIEWERPYTHON-187)
- [Fix] 在 26.9.0 中包含已知 CVE 的组件: .NET runtime 10.0.12、System.Formats.Asn1 8.0.0、System.Security.Cryptography.Pkcs 8.0.0 和 libpng 1.6.44(引擎 Slides 库中的 ICU 59 保持不变) (VIEWERPYTHON-165)
- [Fix] Python:对不存在的选项进行赋值会被静默忽略 (VIEWERPYTHON-170)
- [Fix] 作为输入传入的文件对象会在调用时被关闭,并在打开进行更新时被清空 (VIEWERPYTHON-188)
- [Fix] 每次传入流或回调的调用都会导致内存增长:它们的 .NET 对象从未释放 (VIEWERPYTHON-189)
- [Fix] 在第一次 Viewer 调用后分叉的进程导致父进程因段错误崩溃 (VIEWERPYTHON-190)
- [Fix] 使用 Python 函数渲染到内存失败:“cannot wrap function as CreatePageStream” (VIEWERPYTHON-191)
- [Fix] 并行线程中的渲染可能导致
BytesIO输出为空或抛出另一个线程的异常 (VIEWERPYTHON-192) - [Fix] 在 Windows 上,包在机器上已安装的 .NET 上运行,并且在另一个 GroupDocs Python 包旁边失败 (VIEWERPYTHON-193)
- [Fix] 在 Linux 上,引擎的字体缓存被写入工作目录 (VIEWERPYTHON-194)
- [Fix] 在 Python 3.10 及更早版本中,
SpooledTemporaryFile(如 FastAPI 的UploadFile.file)无法传递给Viewer(VIEWERPYTHON-198)
新功能
在浏览器中打开文档
该包安装了 groupdocs-viewer 命令。它在你的机器上启动一个小型 Web 服务器,并在浏览器中打开 GroupDocs.Viewer 的 Web UI,提供页面导航、缩放、旋转、缩略图、搜索、打印、PDF 下载以及暗色主题。服务器仅使用 Python 标准库,因此没有额外依赖,并可在 Python 3.5 到 3.14 上运行:
groupdocs-viewer # browse the current folder
groupdocs-viewer ~/documents # browse a folder and its subfolders
groupdocs-viewer report.docx -t png # open one file, rendered as PNG pages
python -m groupdocs.viewer --version
它接受 .NET 工具 GroupDocs.Viewer CLI 的选项——-p/--port(默认 8080,或在占用时使用空闲端口),-b/--bind,--no-open,-l/--license-path,-t/--viewer-type html|png|jpg——以及 --allow-host、--cache-dir 和 -v/--verbose。它面向本机使用者:它打印并打开的地址携带会话令牌,其他机器用户和其他网站无法使用,除非另行指定,否则它监听 127.0.0.1。给定一个文件时,仅提供该文件的服务。文件不可上传,且修改后的文件会重新渲染,而不是从缓存提供。UI 基于 GroupDocs.Viewer UI 26.10.0,使用 Angular 22。要在 FastAPI、Flask 或 Django 应用中嵌入查看器,请使用 groupdocs-viewer-net-ui 包。
取消正在运行的渲染
view()、get_view_info()、get_attachments() 和 save_attachment() 接受 cancellation_token 参数,且 CancellationTokenSource、CancellationToken 和 OperationCanceledError 可从 groupdocs.viewer 导入。可在其他线程中使用 source.cancel() 取消,或使用 cancel_after 在若干秒后取消:
from groupdocs.viewer import CancellationTokenSource, OperationCanceledError, Viewer
from groupdocs.viewer.options import HtmlViewOptions
source = CancellationTokenSource()
source.cancel_after(30) # seconds; or source.cancel() from another thread
try:
with Viewer("large.xlsx") as viewer:
viewer.view(HtmlViewOptions.for_embedded_resources("page_{0}.html"),
cancellation_token=source.token)
except OperationCanceledError:
print("cancelled")
引擎在渲染开始前检查令牌,并在将所有页面渲染为 HTML、PNG 或 JPG 时于页面之间检查。对 PDF 的渲染以及对特定页面列表的渲染在渲染开始后会一直运行至结束。
使用 Python 函数渲染到内存
在 26.9 中,所有不写入文件的渲染方式均失败:传递给 PngViewOptions、JpgViewOptions、PdfViewOptions 或 HtmlViewOptions.for_embedded_resources / for_external_resources 的函数都会抛出 “cannot wrap function as CreatePageStream”。现在函数和工厂对象均可工作,并且在 view() 返回时每个 BytesIO 已被填充:
import io
from groupdocs.viewer import Viewer
from groupdocs.viewer.options import PdfViewOptions, PngViewOptions
pages = {}
def create_page_stream(page_number):
pages[page_number] = io.BytesIO()
return pages[page_number]
pdf = io.BytesIO()
with Viewer("document.docx") as viewer:
viewer.view(PngViewOptions(create_page_stream))
viewer.view(PdfViewOptions(lambda: pdf))
print({number: len(page.getvalue()) for number, page in pages.items()}, len(pdf.getvalue()))
LicenseError 替代静默评估模式
未能应用的许可证——如已过期的订阅、针对其他产品的许可证、缺失的文件——过去会被忽略,所有渲染都会悄然以评估模式输出:两页并在每页显示提示。现在每次 Viewer 调用都会抛出 LicenseError,直至成功应用许可证。错误信息会列出变量、来源以及引擎的原因:
from groupdocs.viewer import LicenseError, Viewer
from groupdocs.viewer.options import PdfViewOptions
try:
with Viewer("document.docx") as viewer:
viewer.view(PdfViewOptions("output.pdf"))
except LicenseError as error:
print(error)
GROUPDOCS_LIC_STRICT=0 会将失败转为 LicenseWarning 并进入评估模式。在代码中使用 License().set_license(...) 应用许可证可结束拒绝。
HTML 输出中的 CAD 图层
设置 CadOptions.enable_layered_rendering 后,渲染为 HTML 的 CAD 图纸会将每个图层的对象归入 id 为该图层名称的元素下,从而页面可以在不重新渲染的情况下显示或隐藏图层:
from groupdocs.viewer import Viewer
from groupdocs.viewer.options import HtmlViewOptions
options = HtmlViewOptions.for_embedded_resources("page_{0}.html")
options.cad_options.enable_layered_rendering = True
with Viewer("drawing.dwg") as viewer:
viewer.view(options)
PDF 渲染为 HTML 时的泰文文本
PdfOptions.fix_thai_marks 修复了 Safari 和 iOS 浏览器在将 PDF 渲染为带嵌入资源的 HTML 时出现的孤立泰文标记。此功能可能显著降低渲染速度,默认情况下为关闭状态:
from groupdocs.viewer import Viewer
from groupdocs.viewer.options import HtmlViewOptions
options = HtmlViewOptions.for_embedded_resources("page_{0}.html")
options.pdf_options.fix_thai_marks = True
with Viewer("thai.pdf") as viewer:
viewer.view(options)
代码示例
以下示例演示如何将 PDF 文档渲染为带泰文标记修正的 HTML,并将输出页面保存到内存流中:
import io
from groupdocs.viewer import Viewer
from groupdocs.viewer.options import HtmlViewOptions
def create_page_stream(page_number):
return io.BytesIO()
pages = []
with Viewer("document.pdf") as viewer:
options = HtmlViewOptions.for_embedded_resources("page_{0}.html")
options.pdf_options.fix_thai_marks = True
viewer.view(options, page_numbers=[1, 2, 3])
print("Rendering completed.")
如何获取更新
PyPI
通过 PyPI 升级到最新的 groupdocs-viewer-net 包: GroupDocs.Viewer for Python v26.11
pip install groupdocs-viewer-net==26.11.0
直接下载
从 GroupDocs.Viewer for Python v26.11 发布页面下载 wheel 包和文档。