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:
| Renderer | Handles |
|---|---|
ParagraphRenderer | Renders LDM paragraphs into PDF — handles alignment, spacing, indentation |
RunRenderer | Renders formatted runs — text segments with fonts, colors, and links |
ShapeRenderer | Renders shapes, images, and positioned elements |
TableRenderer | Renders 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
PdfSaveOptionswhen 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
| Issue | Cause | Fix |
|---|---|---|
| Fonts differ in PDF | Font not available on the system | Install the required fonts or use font substitution |
| Tables misaligned | Complex nested table structures | Simplify table nesting in the source document |
| Images missing | Image data not embedded in source | Ensure 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 / Method | Description |
|---|---|
PdfSaveOptions | Options for saving documents as PDF |
ParagraphRenderer | Renders LDM paragraphs into PDF |
RunRenderer | Renders formatted runs (text with fonts, colors, links) |
ShapeRenderer | Renders shapes, images, and positioned elements |
TableRenderer | Renders LDM tables into PDF |
SaveFormat.PDF | Constant for PDF save format |
Document.save(path) | Save document to the specified format |