Working with PDF Writer

Working with PDF Writer

Aspose.Words FOSS for Python exports documents to PDF through a dedicated rendering pipeline. The pipeline converts each LDM element — paragraphs, runs, shapes, and tables — into PDF output using specialized renderers.


Basic PDF Export

Save any loaded document to PDF using the save() method:

import aspose.words_foss as aw

doc = aw.Document("input.docx")
doc.save("output.pdf")

The save format is determined automatically from the file extension. For explicit control, use SaveFormat:

import aspose.words_foss as aw

doc = aw.Document("input.doc")
doc.save("output.pdf", aw.SaveFormat.PDF)

Using PdfSaveOptions

Configure the PDF output with PdfSaveOptions:

import aspose.words_foss as aw
from aspose.words_foss.saving import PdfSaveOptions

doc = aw.Document("input.docx")
opts = PdfSaveOptions()
doc.save("output.pdf", opts)

PDF Rendering Pipeline

The PDF writer uses four specialized renderers internally:

RendererHandles
ParagraphRendererRenders LDM paragraphs into PDF — handles alignment, spacing, indentation
RunRendererRenders formatted runs — text segments with fonts, colors, and links
ShapeRendererRenders shapes, images, and positioned elements
TableRendererRenders LDM tables — cell borders, merges, and padding

These renderers work together to produce the final PDF. You interact with them indirectly through Document.save().


Converting Multiple Formats to PDF

The PDF writer accepts input from any supported reader (DOCX, DOC, RTF, TXT, Markdown):

import aspose.words_foss as aw

for input_file in ["report.docx", "legacy.doc", "notes.rtf", "readme.txt"]:
    doc = aw.Document(input_file)
    pdf_name = input_file.rsplit(".", 1)[0] + ".pdf"
    doc.save(pdf_name)
    print(f"Saved {pdf_name}")

Tips and Best Practices

  • Use PdfSaveOptions when you need to customize the PDF output beyond defaults.
  • For batch conversion, load and save in a loop — the renderers are stateless and reusable across documents.
  • The PDF writer preserves bold, italic, underline, font sizes, and colors from the source document.
  • Table cell merges (CellMerge) are rendered correctly in PDF output.
  • Test with representative documents before deploying to production — complex layouts may need adjustment.

Common Issues

IssueCauseFix
Fonts differ in PDFFont not available on the systemInstall the required fonts or use font substitution
Tables misalignedComplex nested table structuresSimplify table nesting in the source document
Images missingImage data not embedded in sourceEnsure images are embedded, not externally linked

FAQ

Which input formats can be exported to PDF?

DOCX, DOC, RTF, TXT, and Markdown files can all be loaded and saved as PDF.

Does the PDF writer support page headers and footers?

Headers and footers from the source document are included in the PDF output when present in the LDM.

Can I control PDF page size and orientation?

Page setup properties (size, orientation, margins) from the source document are preserved in the PDF. Modify PageSetup on the Document sections before saving to change these values.


API Reference Summary

Class / MethodDescription
PdfSaveOptionsOptions for saving documents as PDF
ParagraphRendererRenders LDM paragraphs into PDF
RunRendererRenders formatted runs (text with fonts, colors, links)
ShapeRendererRenders shapes, images, and positioned elements
TableRendererRenders LDM tables into PDF
SaveFormat.PDFConstant for PDF save format
Document.save(path)Save document to the specified format