Export PDF — Aspose.Note FOSS pentru Python
Aspose.Note FOSS for Python supports exporting loaded .one documente în PDF prin Document.Save(). Redarea PDF este furnizată de opționalul ReportLab bibliotecă. Acesta este singurul format de salvare implementat în prezent; alte SaveFormat valorile cresc UnsupportedSaveFormatException.
Precondiții
Instalați biblioteca cu [pdf] extra pentru a include ReportLab:
pip install "aspose-note[pdf]"Verificați:
python -c "from aspose.note import Document, SaveFormat; print('PDF export ready')"Export de bază
Exportă toate paginile unui document într-un singur fișier PDF:
from aspose.note import Document, SaveFormat
doc = Document("MyNotes.one")
doc.Save("output.pdf", SaveFormat.Pdf)Paginile apar în PDF în aceeași ordine în care apar în DOM.
Folosind PdfSaveOptions
PdfSaveOptions oferă un control fin asupra exportului. Transmite-l în locul celui gol SaveFormat enum:
from aspose.note import Document
from aspose.note.saving import PdfSaveOptions
opts = PdfSaveOptions()
doc = Document("MyNotes.one")
doc.Save("output.pdf", opts)Notă despre PageIndex / PageCount: Aceste câmpuri există pe PdfSaveOptions dar sunt nu este transmis exportatorului PDF în v26.3.1 și nu au niciun efect. Întregul document este întotdeauna exportat.
Referință PdfSaveOptions
| Proprietate | Tip | Implicit | Descriere |
|---|---|---|---|
PageIndex | int | 0 | Câmpul există, dar nu este transmis exportatorului PDF în v26.3.1 (nu are efect) |
PageCount | `int | None` | None |
Export în lot
Convertește fiecare .one fișier dintr-un director în 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}")Încarcă din flux, Salvează în fișier
Combină încărcarea bazată pe flux cu ieșirea PDF bazată pe fișier:
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)Obține octeții PDF în memorie
Document.Save() acceptă un flux binar direct: nu este necesar niciun fișier temporar:
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")Valori SaveFormat acceptate
| SaveFormat | Stare |
|---|---|
SaveFormat.Pdf | Implementat |
Erori comune
| Eroare | Cauză | Remediere |
|---|---|---|
ImportError: No module named 'reportlab' | [pdf] extra nu este instalat | pip install "aspose-note[pdf]" |
UnsupportedSaveFormatException | S-a utilizat SaveFormat non-PDF | Folosiți SaveFormat.Pdf doar |
IncorrectPasswordException | Fișier .one criptat | Folosiți un fișier necriptat |
FileNotFoundError | Calea .one de intrare greșită | Verificați calea cu Path.exists() |
| Eroare de permisiune la ieșire | Directorul de ieșire nu este scriibil | Verificați permisiunile directorului de ieșire |