Coverage for src / local_deep_research / exporters / pdf_exporter.py: 100%

33 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-14 23:55 +0000

1"""PDF export service wrapper. 

2 

3This module provides the PDFExporter class that wraps the existing 

4PDFService implementation from web/services/pdf_service.py. 

5 

6The existing PDFService is kept unchanged - this is just a wrapper 

7to integrate it with the modular exporter registry. 

8""" 

9 

10from typing import Optional 

11 

12from loguru import logger 

13 

14from .base import BaseExporter, ExportOptions, ExportResult 

15from .registry import ExporterRegistry 

16 

17 

18@ExporterRegistry.register 

19class PDFExporter(BaseExporter): 

20 """PDF exporter wrapping the existing PDFService implementation. 

21 

22 This exporter delegates to the existing PDFService which uses 

23 WeasyPrint for PDF generation. No changes are made to the 

24 underlying PDF generation logic. 

25 """ 

26 

27 def __init__(self): 

28 """Initialize PDF exporter.""" 

29 # Import here to avoid circular imports 

30 from ..web.services.pdf_service import get_pdf_service 

31 

32 self._pdf_service = get_pdf_service() 

33 

34 @property 

35 def format_name(self) -> str: 

36 return "pdf" 

37 

38 @property 

39 def file_extension(self) -> str: 

40 return ".pdf" 

41 

42 @property 

43 def mimetype(self) -> str: 

44 return "application/pdf" 

45 

46 def export( 

47 self, 

48 markdown_content: str, 

49 options: Optional[ExportOptions] = None, 

50 ) -> ExportResult: 

51 """Convert markdown content to PDF using existing PDFService. 

52 

53 Args: 

54 markdown_content: The markdown text to convert 

55 options: Optional export options (title, metadata, custom_css) 

56 

57 Returns: 

58 ExportResult with PDF content as bytes, filename, and mimetype 

59 

60 Raises: 

61 ValueError: If content exceeds maximum size limit 

62 """ 

63 try: 

64 # Check content size limit to prevent OOM errors 

65 self._validate_content_size(markdown_content) 

66 

67 options = options or ExportOptions() 

68 

69 # Prepend title if needed (for document formats like PDF) 

70 markdown_content = self._prepend_title_if_needed( 

71 markdown_content, options.title 

72 ) 

73 

74 # Extract custom_css if provided 

75 custom_css = None 

76 if options.custom_options: 

77 custom_css = options.custom_options.get("custom_css") 

78 

79 # Delegate to existing PDFService 

80 pdf_bytes = self._pdf_service.markdown_to_pdf( 

81 markdown_content, 

82 title=options.title, 

83 metadata=options.metadata, 

84 custom_css=custom_css, 

85 ) 

86 

87 filename = self._generate_safe_filename(options.title) 

88 

89 logger.info( 

90 f"Generated PDF in memory, size: {len(pdf_bytes)} bytes" 

91 ) 

92 

93 return ExportResult( 

94 content=pdf_bytes, 

95 filename=filename, 

96 mimetype=self.mimetype, 

97 ) 

98 

99 except Exception: 

100 logger.exception("Error generating PDF") 

101 raise