🔍 Code Extractor

function add_formatted_content_to_pdf

Maturity: 47

Processes markdown elements and adds them to a PDF document story with appropriate formatting, handling headers, paragraphs, lists, and tables.

File:
/tf/active/vicechatdev/vice_ai/complex_app.py
Lines:
1608 - 1637
Complexity:
moderate

Purpose

This function serves as a content formatter for PDF generation using ReportLab. It iterates through a list of parsed markdown elements and converts them into properly styled PDF components (Paragraphs, Spacers, Tables). It handles different element types including headers (levels 1-3), paragraphs with inline markdown formatting, bulleted and numbered lists, and tables. The function is designed to work as part of a larger PDF generation pipeline where markdown content needs to be rendered with consistent styling.

Source Code

def add_formatted_content_to_pdf(story, elements, styles):
    """Add processed markdown elements to PDF with proper formatting"""
    from reportlab.platypus import Paragraph, Spacer
    
    for element in elements:
        if element['type'] == 'header':
            level = min(element['level'], 3)  # Use appropriate heading styles
            if level == 1:
                style_name = 'CustomHeading1'
            elif level == 2:
                style_name = 'CustomHeading2'
            else:
                style_name = 'CustomHeading3'
            story.append(Paragraph(element['content'], styles[style_name]))
            story.append(Spacer(1, 12))
            
        elif element['type'] == 'paragraph':
            formatted_text = convert_markdown_to_html(element['content'])
            story.append(Paragraph(formatted_text, styles['Normal']))
            story.append(Spacer(1, 6))
            
        elif element['type'] in ['list_item', 'numbered_list_item']:
            bullet = '• ' if element['type'] == 'list_item' else '1. '
            formatted_text = bullet + convert_markdown_to_html(element['content'])
            story.append(Paragraph(formatted_text, styles['Normal']))
            
        elif element['type'] == 'table':
            print(f"DEBUG: Adding table to PDF with {len(element['content'])} rows")
            add_table_to_pdf(story, element['content'])
            story.append(Spacer(1, 12))

Parameters

Name Type Default Kind
story - - positional_or_keyword
elements - - positional_or_keyword
styles - - positional_or_keyword

Parameter Details

story: A ReportLab story list (typically from SimpleDocTemplate) that accumulates PDF flowable elements. This is a mutable list that gets modified in-place by appending formatted content elements.

elements: A list of dictionaries representing parsed markdown elements. Each dictionary must have a 'type' key (values: 'header', 'paragraph', 'list_item', 'numbered_list_item', 'table') and a 'content' key. Headers also require a 'level' key (integer). Tables should have content as a list of rows.

styles: A ReportLab StyleSheet dictionary containing predefined paragraph styles. Must include keys: 'CustomHeading1', 'CustomHeading2', 'CustomHeading3', and 'Normal'. Typically obtained from getSampleStyleSheet() and customized.

Return Value

This function returns None. It modifies the 'story' parameter in-place by appending formatted PDF elements to it.

Dependencies

  • reportlab

Required Imports

from reportlab.platypus import Paragraph, Spacer

Conditional/Optional Imports

These imports are only needed under specific conditions:

from reportlab.platypus import Paragraph, Spacer

Condition: imported inside the function at runtime, always required when function executes

Required (conditional)

Usage Example

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.pagesizes import letter
from reportlab.lib.colors import black

# Setup PDF document
pdf_file = 'output.pdf'
doc = SimpleDocTemplate(pdf_file, pagesize=letter)
story = []

# Create styles
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='CustomHeading1', fontSize=18, textColor=black, spaceAfter=12))
styles.add(ParagraphStyle(name='CustomHeading2', fontSize=14, textColor=black, spaceAfter=10))
styles.add(ParagraphStyle(name='CustomHeading3', fontSize=12, textColor=black, spaceAfter=8))

# Define markdown elements
elements = [
    {'type': 'header', 'level': 1, 'content': 'Main Title'},
    {'type': 'paragraph', 'content': 'This is a paragraph with **bold** text.'},
    {'type': 'list_item', 'content': 'First bullet point'},
    {'type': 'numbered_list_item', 'content': 'First numbered item'},
    {'type': 'table', 'content': [['Header1', 'Header2'], ['Data1', 'Data2']]}
]

# Add formatted content
add_formatted_content_to_pdf(story, elements, styles)

# Build PDF
doc.build(story)

Best Practices

  • Ensure the 'story' list is initialized before calling this function
  • Pre-configure all required custom styles (CustomHeading1, CustomHeading2, CustomHeading3) in the styles dictionary
  • Validate that elements list contains properly structured dictionaries with required keys before passing to this function
  • Implement convert_markdown_to_html() and add_table_to_pdf() helper functions in the same module
  • Header levels are capped at 3 to prevent style lookup errors; levels 4+ will use CustomHeading3
  • The function prints debug messages for tables; consider using proper logging instead of print statements in production
  • List items are simplified with static bullets ('•' and '1.'); for proper numbered lists, implement counter logic
  • Consider error handling for missing style keys or malformed element dictionaries

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function add_formatted_content_to_pdf_v1 95.7% similar

    Converts processed markdown elements into formatted PDF content by adding paragraphs, headers, lists, and tables to a ReportLab story object with appropriate styling.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function add_table_to_pdf 76.0% similar

    Adds a formatted table to a ReportLab PDF document with automatic text wrapping, column width calculation, and alternating row colors.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function add_table_to_pdf_v1 75.4% similar

    Adds a formatted table to a PDF document story with proper text wrapping, styling, and header formatting using ReportLab's platypus components.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function add_formatted_content_to_word_v1 73.4% similar

    Converts processed markdown elements into formatted content within a Microsoft Word document, handling headers, paragraphs, lists, tables, and code blocks with appropriate styling.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function add_formatted_content_to_word 73.1% similar

    Converts processed markdown elements into formatted content within a Word document, handling headers, paragraphs, lists, tables, and code blocks with appropriate styling.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
← Back to Browse