PDF eksportas — Aspose.Note FOSS for Python

Aspose.Note FOSS for Python palaiko įkeltų .one dokumentų eksportavimą į PDF per Document.Save(). PDF atvaizdavimą teikia papildoma ReportLab biblioteka. Tai yra vienintelis šiuo metu įgyvendintas išsaugojimo formatas; kiti SaveFormat reikšmės sukelia UnsupportedSaveFormatException.


Būtinos sąlygos

Įdiekite biblioteką su [pdf] papildiniu, kad įtrauktumėte ReportLab:

pip install "aspose-note[pdf]"

Patikrinkite:

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

Pagrindinis eksportavimas

Eksportuoti visus dokumento puslapius į vieną PDF failą:

from aspose.note import Document, SaveFormat

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

Puslapiai PDF faile rodomi tokia pačia tvarka, kokia jie yra DOM struktūroje.


Naudojant PdfSaveOptions

PdfSaveOptions suteikia smulkią kontrolę eksportui. Perdėkite jį vietoj paprasto SaveFormat enum:

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

opts = PdfSaveOptions()

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

Pastaba apie PageIndex / PageCount: Šie laukai egzistuoja PdfSaveOptions, tačiau nėra perduodami PDF eksporteriui v26.3.1 ir neturi jokio poveikio. Visas dokumentas visada eksportuojamas.


PdfSaveOptions nuoroda

PropertyTypeDefaultDescription
PageIndexint0Laukas egzistuoja, bet neperduodamas PDF eksportoriui v26.3.1 (neturi jokio poveikio)
PageCountint | NoneNoneLaukas egzistuoja, bet neperduodamas PDF eksportoriui v26.3.1 (neturi jokio poveikio)

Masinis eksportavimas

Konvertuokite kiekvieną .one failą kataloge į 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}")

Įkelti iš srauto, išsaugoti į failą

Kombinuokite srauto pagrindu įkėlimą su failų pagrindu PDF išvestimi:

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)

Gauti PDF baitus atmintyje

Document.Save() priima dvejetainį srautą tiesiogiai: nereikia laikino failo:

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

Palaikomos SaveFormat reikšmės

SaveFormatBūsena
SaveFormat.PdfĮgyvendinta

Dažnosios klaidos

KlaidaPriežastisSprendimas
ImportError: No module named 'reportlab'[pdf] extra neįdiegtapip install "aspose-note[pdf]"
UnsupportedSaveFormatExceptionNaudotas ne-PDF SaveFormatNaudokite tik SaveFormat.Pdf
IncorrectPasswordExceptionUžšifruotas .one failasNaudokite nešifruotą failą
FileNotFoundErrorNeteisingas įvesties .one keliasPatikrinkite kelią su Path.exists()
Leidimo klaida išvestyjeIšvesties katalogas neįrašomasPatikrinkite išvesties katalogo leidimus
 Lietuvių