Exportació PDF — Aspose.Note FOSS per a Python

Aspose.Note FOSS for Python supports exporting loaded .one documents a PDF via Document.Save(). El renderitzat de PDF és proporcionat per l’opcional ReportLab biblioteca. Aquest és l’únic format de desament implementat actualment; altres SaveFormat valors generen UnsupportedSaveFormatException.


Requisits previs

Instal·la la biblioteca amb el [pdf] extra per incloure ReportLab:

pip install "aspose-note[pdf]"

Verifica:

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

Exportació bàsica

Exporta totes les pàgines d’un document a un únic fitxer PDF:

from aspose.note import Document, SaveFormat

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

Les pàgines apareixen al PDF en el mateix ordre que apareixen al DOM.


Utilitzant PdfSaveOptions

PdfSaveOptions proporciona un control detallat sobre l’exportació. Passa’l en lloc del simple SaveFormat enum:

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

opts = PdfSaveOptions()

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

Nota sobre PageIndex / PageCount: Aquests camps existeixen a PdfSaveOptions però són no s’envia a l’exportador PDF a la v26.3.1 i no tenen cap efecte. Sempre s’exporta tot el document.


Referència de PdfSaveOptions

PropietatTipusPer defecteDescripció
PageIndexint0El camp existeix però no s’envia a l’exportador PDF a la versió v26.3.1 (no té cap efecte)
PageCount`intNone`None

Exportació per lots

Converteix cada .one fitxer d’un directori a 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}")

Carrega des d’un flux, desa a fitxer

Combina la càrrega basada en flux amb la sortida PDF basada en fitxer:

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)

Obté bytes PDF en memòria

Document.Save() accepta un flux binari directament: no cal fitxer temporal:

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

Valors de SaveFormat compatibles

SaveFormatEstat
SaveFormat.PdfImplementat

Errors comuns

ErrorCausaCorrecció
ImportError: No module named 'reportlab'[pdf] extra no instal·latpip install "aspose-note[pdf]"
UnsupportedSaveFormatExceptionS’ha utilitzat SaveFormat no PDFUtilitza SaveFormat.Pdf només
IncorrectPasswordExceptionFitxer .one encriptatUtilitza un fitxer sense encriptar
FileNotFoundErrorCamí .one d’entrada incorrecteVerifica el camí amb Path.exists()
Error de permís a la sortidaDirectori de sortida no es pot escriureComprova els permisos del directori de sortida
 Català