我們很高興宣布 GroupDocs.Viewer for Python v26.11 已於 2026 年 11 月 發佈。此版本將底層的 GroupDocs.Viewer 引擎升級至 .NET 10 構建的 26.10 版,並為 Python API 帶來重大改進——包括能夠取消長時間執行的渲染、渲染至記憶體串流,以及使用全新 groupdocs-viewer CLI 命令直接在瀏覽器中開啟文件。它同時解決了與記憶體洩漏、進程分叉和檔案處理相關的關鍵穩定性問題。

此版本的新功能

  • [功能] 新增 CadOptions.enable_layered_rendering 以在 HTML 輸出中顯示和隱藏 CAD 圖層,無需重新渲染 (VIEWERPYTHON-180)
  • [功能] 新增 PdfOptions.fix_thai_marks 以在將 PDF 渲染為 HTML 時修復孤立的泰文標記 (VIEWERPYTHON-181)
  • [功能] 使用 CancellationTokenSource 與 view(options, cancellation_token=...) 取消正在執行的渲染 (VIEWERPYTHON-182)
  • [功能] 新增 groupdocs-viewer 命令以在瀏覽器中開啟文件或資料夾 (VIEWERPYTHON-199)
  • [增強] 將 GroupDocs.Viewer 引擎從 26.8 更新至 26.10,使用其 .NET 10 構建 (VIEWERPYTHON-183)
  • [增強] 當 GROUPDOCS_LIC_PATH 中的授權無法套用時拋出 LicenseError,而不是靜默以評估模式運行 (VIEWERPYTHON-184)
  • [增強] 以指標方式而非 base64 JSON 將串流輸入與 BytesIO 輸出傳遞給 .NET (VIEWERPYTHON-185)
  • [增強] 從 groupdocs-shared-net 套件安裝所有 GroupDocs Python 套件共用的模組 (VIEWERPYTHON-186)
  • [增強] 將 docstring 連結至 Python 文件,並為每個重載提供呼叫範例 (VIEWERPYTHON-187)
  • [修復] 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)
  • [修復] Python:指派不存在的選項會被靜默忽略 (VIEWERPYTHON-170)
  • [修復] 作為輸入傳遞的檔案物件會在呼叫時被關閉,且在開啟更新時被清空 (VIEWERPYTHON-188)
  • [修復] 每次傳遞串流或回呼時記憶體會增長:其 .NET 物件從未釋放 (VIEWERPYTHON-189)
  • [修復] 第一次 Viewer 呼叫後分叉的進程導致父進程因段錯誤而崩潰 (VIEWERPYTHON-190)
  • [修復] 使用 Python 函式渲染至記憶體失敗:「cannot wrap function as CreatePageStream」 (VIEWERPYTHON-191)
  • [修復] 在平行執行緒中執行的渲染可能導致 BytesIO 輸出為空或拋出其他執行緒的例外 (VIEWERPYTHON-192)
  • [修復] 在 Windows 上,套件在機器上已安裝的 .NET 上執行,且在另一個 GroupDocs Python 套件旁失敗 (VIEWERPYTHON-193)
  • [修復] 在 Linux 上,引擎的字型快取寫入工作目錄 (VIEWERPYTHON-194)
  • [修復] 在 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」錯誤。現在函式與工廠物件皆可使用,且每個 BytesIO 會在 view() 返回時填滿:

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 檔與文件。

資源