PDF Analysis Export
Relevant source files
The following files were used as context for generating this wiki page:
The PDF Analysis Export system provides a robust mechanism for transforming structured YouTube content intelligence into portable, professional documents. This feature enables users to download synthesized reports of video analyses in two distinct formats: an Executive Summary for rapid insight and a Full Synthesis Report containing multi-dimensional intelligence data.
The export process is governed by a tier-based access model where the full multi-dimensional report is restricted to "Pro", "Enterprise", and "Admin" users, while basic "Executive Summaries" are available to all authenticated users. The system leverages pdfkit for document generation and integrates a hallucination filter to ensure high data integrity in the final output.
Sources: web/app/api/analyses/[id]/export/route.ts:47-112, web/components/containers/DashboardContainer.tsx:215-230
Architecture and Data Flow
The export functionality is implemented as a dynamic API route that handles authentication, authorization, and PDF generation. It interacts with the Supabase database to retrieve stored analysis markdown and uses the pdfkit library to render the document structure.
Export Request Pipeline
The following diagram illustrates the sequence of events from a user triggering an export to receiving the final PDF file.
Functional Components
1. API Endpoint and Security
The endpoint resides at /api/analyses/[id]/export. It enforces strict security checks, including user authentication and ownership verification, ensuring that users can only export analyses they created.
- Tier Validation: The system uses
getUserTier(userId)to determine if a user has permission for a "full" report. - Permitted Tiers: Only
pro,enterprise, andadmintiers can access the full 11-dimension report. - Filename Sanitization: Filenames are stripped of invalid characters and constrained to 120 characters to prevent header injection.
Sources: web/app/api/analyses/[id]/export/route.ts:10-45, web/app/api/analyses/[id]/export/route.ts:79-88
2. PDF Generation Logic
The system uses two primary rendering functions based on the requested scope:
| Function | Scope | Description |
|---|---|---|
exportSummaryPDF | summary | Generates a 1-page overview limited to approximately 2400 characters. |
exportFullPDF | full | Generates a comprehensive report including a Table of Contents and all 11 dimensions. |
Executive Summary Generation
The summary generator parses the markdown and extracts headers and body text until the MAX_CHARS limit is reached. It applies specific styling:
- Title: 18pt Helvetica-Bold
- Headers: 11pt Helvetica-Bold (Color:
#0b0e14) - Body: 9.5pt Helvetica (Color:
#333333)
Full Synthesis Report Generation
The full report includes a Table of Contents generated by scanning headers (#, ##, ###) in the markdown. Each dimension from the Ultimate Content Intelligence & Implementation System (UCIS) is rendered with distinct font sizes and weights.
Sources: web/app/api/analyses/[id]/export/route.ts:133-176, web/app/api/analyses/[id]/export/route.ts:178-217
Data Models and Schemas
Exportable Analysis Interface
The system expects a specific data structure from the database to populate the PDF fields.
interface ExportableAnalysis {
title?: string | null;
channel_title?: string | null;
created_at?: string | null;
analysis_markdown?: string | null;
}
Sources: web/app/api/analyses/[id]/export/route.ts:117-122
UCIS Dimensions
The exported content follows the UCIS v5.1 structure, which includes 11 specific knowledge dimensions.
| Dimension | Title | Purpose |
|---|---|---|
| 1 | Apex Intelligence | 60-second ROI and executive summary. |
| 5 | Core Intelligence | Substance, insights, and power quotes. |
| 8 | Semantic Foundation | Knowledge Graph nodes and relations. |
| 11 | Commercial Yield | Monetization and revenue profiling. |
Sources: web/lib/prompts/ucis-v5.1.ts:101-260, web/lib/tests/ucis-v5-validator.test.ts:25-36
Implementation Details
Hallucination Filtering
Before rendering, the content is passed through filterHallucinationContent. This utility is critical for ensuring that the deterministic, "closed-universe" requirement of the UCIS system is maintained in the final export, preventing the inclusion of fabricated data.
Sources: web/app/api/analyses/[id]/export/route.ts:138, web/lib/prompts/ucis-v5.1.ts:45-65
Content Processing Flow
The following flow chart describes how markdown is converted into a PDF document.
Error Handling
The export route implements specific error codes and Sentry integration:
AUTH_UNAUTHORIZED: Triggered if the user session is missing.NOT_FOUND: Triggered if the analysis ID does not exist or belong to the user.ANALYSIS_GENERATION_FAILED: Triggered ifanalysis_markdownis empty or incomplete.UNHANDLED_EXCEPTION: General catch-all for stream or PDFKit failures.
Sources: web/app/api/analyses/[id]/export/route.ts:54-110
Conclusion
The PDF Analysis Export system is a sophisticated reporting tool that bridges the gap between AI-generated synthesis and professional business documentation. By enforcing tier-based limits and utilizing structured prompts (UCIS v5.1), the system ensures that high-value intelligence remains a premium feature while providing essential utility to all users through the Executive Summary format.