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ính | Kiểu | Mặc định | Mô tả |
|---|---|---|---|
PageIndex | int | 0 | Trườ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 | `int | None` | 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ợ
| SaveFormat | Trạng thái |
|---|---|
SaveFormat.Pdf | Đã triển khai |
Lỗi thường gặp
| Lỗi | Nguyên nhân | Sửa chữa |
|---|---|---|
ImportError: No module named 'reportlab' | [pdf] extra chưa được cài đặt | pip install "aspose-note[pdf]" |
UnsupportedSaveFormatException | Đã sử dụng SaveFormat không phải PDF | Sử dụng SaveFormat.Pdf chỉ |
IncorrectPasswordException | Tệp .one được mã hóa | Sử dụng tệp không được mã hóa |
FileNotFoundError | Đường dẫn .one đầu vào sai | Xác minh đường dẫn với Path.exists() |
| Lỗi quyền trên đầu ra | Thư mục đầu ra không thể ghi | Kiểm tra quyền của thư mục đầu ra |