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 LdmPdfWriter:

import aspose.words_foss as aw
from aspose.words_foss.pdf_writer import LdmPdfWriter

doc = aw.Document("input.docx")
LdmPdfWriter().write(doc, "output.pdf")

The input format is detected automatically when loading — Document accepts DOCX, DOC, RTF, TXT, and Markdown files, and LdmPdfWriter always produces PDF output regardless of the input format:

import aspose.words_foss as aw
from aspose.words_foss.pdf_writer import LdmPdfWriter

doc = aw.Document("input.doc")
LdmPdfWriter().write(doc, "output.pdf")

Using PdfSaveOptions

Configure the PDF output by passing PdfSaveOptions to the writer’s constructor:

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

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

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 LdmPdfWriter.


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
from aspose.words_foss.pdf_writer import LdmPdfWriter

writer = LdmPdfWriter()
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"
    writer.write(doc, 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
LdmPdfWriter().write(doc, path)Save document to PDF

See Also

 English