Xuất PDF — Aspose.Note FOSS cho Python

Aspose.Note FOSS for Python supports exporting loaded .one tài liệu sang PDF qua Document.Save(). Việc hiển thị PDF được cung cấp bởi tùy chọn ReportLab thư viện. Đây là định dạng lưu duy nhất hiện đang được triển khai; các định dạng khác SaveFormat giá trị gây ra UnsupportedSaveFormatException.


Yêu cầu trước

Cài đặt thư viện với [pdf] extra để kéo ReportLab vào:

pip install "aspose-note[pdf]"

Xác minh:

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

Xuất Cơ Bản

Xuất tất cả các trang của tài liệu thành một tệp PDF duy nhất:

from aspose.note import Document, SaveFormat

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

Các trang xuất hiện trong PDF theo cùng thứ tự như chúng xuất hiện trong DOM.


Sử dụng PdfSaveOptions

PdfSaveOptions cung cấp kiểm soát chi tiết đối với việc xuất. Hãy truyền nó thay vì SaveFormat enum:

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

opts = PdfSaveOptions()

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

Lưu ý về PageIndex / PageCount: Các trường này tồn tại trên PdfSaveOptions nhưng là không được chuyển tiếp tới bộ xuất PDF trong v26.3.1 và không có tác dụng. Toàn bộ tài liệu luôn được xuất.


Tham chiếu PdfSaveOptions

Thuộc tínhKiểuMặc địnhMô tả
PageIndexint0Trường tồn tại nhưng không được chuyển tiếp tới bộ xuất PDF trong v26.3.1 (không có tác dụng)
PageCount`intNone`None

Xuất Hàng Loạt

Chuyển đổi mọi .one tệp trong một thư mục sang 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}")

Tải từ Stream, Lưu vào File

Kết hợp việc tải dựa trên stream với xuất PDF dựa trên file:

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)

Lấy dữ liệu PDF trong bộ nhớ

Document.Save() chấp nhận luồng nhị phân trực tiếp: không cần tệp tạm thời:

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")

Các giá trị SaveFormat được hỗ trợ

SaveFormatTrạng thái
SaveFormat.PdfĐã triển khai

Lỗi thường gặp

LỗiNguyên nhânSửa chữa
ImportError: No module named 'reportlab'[pdf] extra chưa được cài đặtpip install "aspose-note[pdf]"
UnsupportedSaveFormatExceptionĐã sử dụng SaveFormat không phải PDFSử dụng SaveFormat.Pdf chỉ
IncorrectPasswordExceptionTệp .one được mã hóaSử dụng tệp không được mã hóa
FileNotFoundErrorĐường dẫn .one đầu vào saiXác minh đường dẫn với Path.exists()
Lỗi quyền trên đầu raThư mục đầu ra không thể ghiKiểm tra quyền của thư mục đầu ra
 Tiếng Việt