function export_document
Flask route handler that exports a document in either DOCX or PDF format, verifying user ownership and document access before generating the export file.
/tf/active/vicechatdev/vice_ai/new_app.py
1539 - 1591
moderate
Purpose
This endpoint allows authenticated users to export their documents in either Microsoft Word (DOCX) or PDF format. It validates document ownership, checks format availability, retrieves all document sections (both text and data), and generates the appropriate export file using specialized export functions. The function handles authentication, authorization, format validation, and error handling for the export process.
Source Code
def export_document(document_id, format):
"""Export document in specified format (docx or pdf)"""
user_email = get_current_user()
# Verify document ownership
document = document_service.get_document(document_id)
if not document or document.owner != user_email:
return jsonify({'error': 'Document not found or access denied'}), 404
if format not in ['docx', 'pdf']:
return jsonify({'error': 'Unsupported format. Use docx or pdf'}), 400
if format == 'docx' and not DOCX_AVAILABLE:
return jsonify({'error': 'DOCX export not available. Install python-docx'}), 500
if format == 'pdf' and not PDF_AVAILABLE:
return jsonify({'error': 'PDF export not available. Install reportlab'}), 500
try:
# Get document with text sections AND data sections
result_tuple = document_service.get_document_with_all_sections(document_id)
if not result_tuple:
return jsonify({'error': 'Failed to load document sections'}), 500
document, text_sections, data_sections = result_tuple
# Debug: Log section order
logger.info(f"Export {format}: Document '{document.title}' has {len(text_sections)} text sections and {len(data_sections)} data sections")
for i, section in enumerate(text_sections):
logger.info(f" Text {i+1}. {section.section_type.value}: '{section.title}'")
for i, section in enumerate(data_sections):
logger.info(f" Data {i+1}. '{section.title}'")
# Generate the export file
if format == 'docx':
file_data = export_to_docx(document, text_sections, data_sections)
filename = f"{document.title}.docx"
mimetype = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
else: # PDF
file_data = export_to_pdf(document, text_sections, data_sections)
filename = f"{document.title}.pdf"
mimetype = 'application/pdf'
return send_file(
BytesIO(file_data),
as_attachment=True,
download_name=filename,
mimetype=mimetype
)
except Exception as e:
logger.error(f"Error exporting document: {e}")
return jsonify({'error': str(e)}), 500
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
document_id |
- | - | positional_or_keyword |
format |
- | - | positional_or_keyword |
Parameter Details
document_id: String identifier for the document to be exported. Used to retrieve the document from the database and verify ownership. Must correspond to an existing document owned by the authenticated user.
format: String specifying the export format. Must be either 'docx' for Microsoft Word format or 'pdf' for PDF format. Any other value will result in a 400 error response.
Return Value
Returns a Flask Response object. On success (200), returns a file download response with the exported document as an attachment with appropriate MIME type ('application/vnd.openxmlformats-officedocument.wordprocessingml.document' for DOCX or 'application/pdf' for PDF). On error, returns a JSON response with an 'error' key and appropriate HTTP status code: 404 if document not found or access denied, 400 if unsupported format, 500 if export libraries unavailable or export fails.
Dependencies
flaskpython-docxreportlabio
Required Imports
from flask import jsonify, send_file
from io import BytesIO
import logging
Conditional/Optional Imports
These imports are only needed under specific conditions:
from docx import Document
Condition: Required for DOCX export functionality. Must be installed via 'pip install python-docx'
Optionalfrom reportlab.lib.pagesizes import letter, A4
Condition: Required for PDF export functionality. Must be installed via 'pip install reportlab'
OptionalUsage Example
# Example Flask route usage (this function is a route handler)
# Client-side request:
import requests
# Assuming authentication is handled via session/cookies
response = requests.get(
'http://localhost:5000/api/documents/doc123/export/pdf',
cookies={'session': 'your_session_cookie'}
)
if response.status_code == 200:
with open('exported_document.pdf', 'wb') as f:
f.write(response.content)
print('Document exported successfully')
else:
print(f"Error: {response.json()['error']}")
# For DOCX export:
response = requests.get(
'http://localhost:5000/api/documents/doc123/export/docx',
cookies={'session': 'your_session_cookie'}
)
if response.status_code == 200:
with open('exported_document.docx', 'wb') as f:
f.write(response.content)
print('Document exported successfully')
Best Practices
- Always verify document ownership before allowing export to prevent unauthorized access
- Check format availability flags (DOCX_AVAILABLE, PDF_AVAILABLE) before attempting export to provide clear error messages
- Use try-except blocks to catch and log export errors gracefully
- Log section information during export for debugging and audit purposes
- Use BytesIO to handle file data in memory rather than writing to disk
- Set appropriate MIME types for different export formats to ensure proper browser handling
- Sanitize document titles when using them as filenames to prevent path traversal issues
- Ensure the require_auth decorator is properly configured to prevent unauthorized access
- Return appropriate HTTP status codes (404 for not found, 400 for bad request, 500 for server errors)
- Consider implementing rate limiting for export endpoints to prevent abuse
- Validate that document_service.get_document_with_all_sections() returns the expected tuple structure before unpacking
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function api_export_document 90.2% similar
-
function export_to_word 74.5% similar
-
function export_report 71.7% similar
-
function api_upload_document_v1 71.0% similar
-
function export_to_pdf_v1 66.8% similar