-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmd_pdf_converter.py
More file actions
882 lines (754 loc) · 29.3 KB
/
md_pdf_converter.py
File metadata and controls
882 lines (754 loc) · 29.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
from fastapi import HTTPException
import markdown
from io import BytesIO
from datetime import datetime
import re
from html import escape
from urllib.parse import urljoin
from bs4 import BeautifulSoup
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.lib.colors import HexColor, black, blue, grey
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak, HRFlowable
from reportlab.platypus import ListFlowable, ListItem, XPreformatted, Image, KeepTogether
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT, TA_JUSTIFY
from reportlab.pdfgen import canvas
from reportlab.platypus.tableofcontents import TableOfContents
import xml.sax.saxutils as saxutils
from pypdf import PdfReader, PdfWriter
from pypdf.generic import DictionaryObject, ArrayObject, NameObject, NumberObject, TextStringObject
# Global list to store pending internal links
_pending_links = []
_bookmark_registry = {}
class InternalLinkParagraph(Paragraph):
"""
Custom Paragraph class that supports internal PDF links.
Parses content for <ilink dest="...">text</ilink> tags and creates clickable regions.
"""
def __init__(self, text, style, **kwargs):
# Store internal links info
self.internal_links = []
# Parse and extract internal links
import re
pattern = r'<ilink dest="([^"]+)">([^<]+)</ilink>'
def replacer(match):
dest = match.group(1)
link_text = match.group(2)
self.internal_links.append(dest)
# Replace with styled text (blue and underlined)
return f'<font color="blue"><u>{link_text}</u></font>'
processed_text = re.sub(pattern, replacer, text)
super().__init__(processed_text, style, **kwargs)
def draw(self):
super().draw()
# Create internal links using bookmarkHorizontal
if self.internal_links:
x, y = self.canv.absolutePosition(0, 0)
w, h = self.width, self.height
for dest in self.internal_links:
try:
# Use bookmarkHorizontal to link to a bookmark
# The key is to not specify the page - let it be resolved later
self.canv.bookmarkHorizontal(dest, x, y + h)
except Exception as e:
# Ignore errors - bookmark might not exist yet
pass
class BookmarkableHeading(Paragraph):
"""A Paragraph with an optional PDF bookmark anchor."""
def __init__(self, text, style, bookmark_name=None):
super().__init__(text, style)
self.bookmark_name = bookmark_name
def draw(self):
# Register this bookmark
if self.bookmark_name:
page_num = self.canv.getPageNumber()
x, y = self.canv.absolutePosition(0, 0)
_bookmark_registry[self.bookmark_name] = {
'page': page_num,
'x': x,
'y': y + self.height # Top of the heading
}
# Create a bookmark page marker
self.canv.bookmarkPage(self.bookmark_name)
super().draw()
def footer(canvas_obj, doc):
"""Add page number footer to each page"""
canvas_obj.saveState()
canvas_obj.setFont('Helvetica', 9)
page_num = canvas_obj.getPageNumber()
text = f"Page {page_num}"
canvas_obj.drawCentredString(letter[0] / 2, 0.5 * inch, text)
canvas_obj.restoreState()
def create_anchor_id(text):
"""
Convert heading text to an anchor ID similar to how markdown generates IDs.
E.g., "Executive Summary" -> "executive-summary"
"Financial Model & Valuation" -> "financial-model--valuation"
"""
# Remove HTML tags
text = re.sub(r'<[^>]+>', '', text)
# Decode HTML entities (e.g., & -> &)
from html import unescape
text = unescape(text)
# Convert to lowercase
text = text.lower()
# Remove & and other special chars, but keep spaces and hyphens
# Markdown removes & leaving adjacent spaces which become --
text = re.sub(r'&', '', text)
# Replace spaces with hyphens
text = re.sub(r'[\s]+', '-', text)
# Remove other special chars except hyphens
text = re.sub(r'[^\w-]', '', text)
# Remove leading/trailing hyphens
text = text.strip('-')
return text
# Allowed inline HTML tags for ReportLab Paragraph
ALLOWED_INLINE = {"b", "strong", "i", "em", "u", "br", "code", "a"}
def to_paragraph_html(node):
"""
Convert an element's inline content to ReportLab mini-HTML.
Preserves bold/italic/inline code/links/line breaks.
"""
if not node:
return ""
parts = []
# Handle node's children
if hasattr(node, 'children'):
for child in node.children:
if getattr(child, "name", None) is None:
# Text node
parts.append(escape(str(child)))
continue
name = child.name.lower()
if name in ("b", "strong"):
parts.append(f"<b>{to_paragraph_html(child)}</b>")
elif name in ("i", "em"):
parts.append(f"<i>{to_paragraph_html(child)}</i>")
elif name == "u":
parts.append(f"<u>{to_paragraph_html(child)}</u>")
elif name == "br":
parts.append("<br/>")
elif name == "code":
# Inline code
code_text = escape(child.get_text())
parts.append(f'<font face="Courier" size="10">{code_text}</font>')
elif name == "a":
# Links - ReportLab uses <link href="">
href = child.get("href") or ""
text = to_paragraph_html(child)
if href and not href.startswith('#'):
# External links work fine with standard link tag
parts.append(f'<link href="{escape(href, quote=True)}" color="blue">{text}</link>')
elif href and href.startswith('#'):
# Internal bookmark links - use custom <ilink> tag
# Strip the # and use the bookmark name as destination
dest_name = href[1:] # Remove the leading #
parts.append(f'<ilink dest="{dest_name}">{text}</ilink>')
else:
# No href - just render text
parts.append(text)
else:
# Unknown tag - just get text
parts.append(escape(child.get_text()))
return "".join(parts)
def build_list(list_el, styles, level=0):
"""
Build a nested list structure with proper support for sublists.
"""
items = []
for li in list_el.find_all('li', recursive=False):
# Get paragraph content (excluding nested lists)
para_html = to_paragraph_html(li)
# Remove text from nested lists to avoid duplication
for sub in li.find_all(['ul', 'ol'], recursive=False):
sub_text = sub.get_text()
para_html = para_html.replace(escape(sub_text), "")
# Create paragraph - check for internal links
has_internal_links = '<ilink dest=' in para_html
if has_internal_links:
p = InternalLinkParagraph(para_html.strip(), styles['CustomListItem'])
else:
p = Paragraph(para_html.strip(), styles['CustomListItem'])
# Handle nested lists
sublists = []
for sub in li.find_all(['ul', 'ol'], recursive=False):
sublists.append(build_list(sub, styles, level + 1))
if sublists:
items.append(ListItem([p] + sublists))
else:
items.append(ListItem(p))
# Use proper bullet characters for unordered lists
if list_el.name == 'ul':
bullet_type = 'bullet'
# Also try with explicit bullet character
start_value = None
else:
bullet_type = '1'
start_value = '1'
return ListFlowable(
items,
bulletType=bullet_type,
start=start_value,
leftIndent=18 + level * 12,
bulletFontName='Symbol' if list_el.name == 'ul' else 'Helvetica'
)
def build_table(table_el, styles, page_width, left_margin, right_margin):
"""
Build a table with proper column widths and header handling.
Ensures tables fit within page margins with professional styling.
"""
data = []
thead = table_el.find('thead')
tbody = table_el.find('tbody')
# Process header
if thead:
head_cells = []
for th in thead.find_all('th'):
cell_html = to_paragraph_html(th)
head_cells.append(Paragraph(cell_html or '', styles['TableHeader']))
if head_cells:
data.append(head_cells)
# Process body rows
body_parent = tbody or table_el
for tr in body_parent.find_all('tr', recursive=False):
cells = []
for td in tr.find_all(['td', 'th'], recursive=False):
cell_html = to_paragraph_html(td)
cells.append(Paragraph(cell_html or '', styles['CustomBody']))
if cells:
data.append(cells)
if not data:
return None
# Determine if we should repeat headers
repeat_rows = 1 if (thead or (data and len(data) > 1)) else 0
# Calculate column widths - distribute evenly within available space
avail_width = page_width - left_margin - right_margin
ncols = max(len(row) for row in data) if data else 1
col_widths = [avail_width / ncols] * ncols
# Create table
t = Table(data, colWidths=col_widths, repeatRows=repeat_rows)
# Apply professional styling
style_commands = [
('GRID', (0, 0), (-1, -1), 0.5, HexColor('#cbd5e1')),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
('LEFTPADDING', (0, 0), (-1, -1), 8),
('RIGHTPADDING', (0, 0), (-1, -1), 8),
('TOPPADDING', (0, 0), (-1, -1), 8),
('BOTTOMPADDING', (0, 0), (-1, -1), 8),
('FONTNAME', (0, 1), (-1, -1), 'Helvetica'),
('FONTSIZE', (0, 1), (-1, -1), 9.5),
('TEXTCOLOR', (0, 1), (-1, -1), HexColor('#374151')), # Body text only, starting from row 1
('ROWBACKGROUNDS', (0, 1), (-1, -1), [HexColor('#ffffff'), HexColor('#f8fafc')]),
]
# Header styling if we have one - applied AFTER body styles to ensure it takes precedence
if repeat_rows:
style_commands.extend([
('BACKGROUND', (0, 0), (-1, 0), HexColor('#1e3a8a')),
('TEXTCOLOR', (0, 0), (-1, 0), HexColor('#ffffff')), # White text for header
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, 0), 10),
('ALIGN', (0, 0), (-1, 0), 'CENTER'),
])
t.setStyle(TableStyle(style_commands))
return t
def build_image(img_el, base_url, max_width):
"""
Build an image flowable with proper scaling.
"""
src = img_el.get('src')
if not src:
return None
try:
# Resolve relative URLs
if base_url:
src = urljoin(base_url, src)
im = Image(src)
# Scale to fit page width if needed
if im.drawWidth > max_width:
scale = max_width / float(im.drawWidth)
im.drawWidth *= scale
im.drawHeight *= scale
return im
except Exception as e:
# If image can't be loaded, return None
print(f"Warning: Could not load image {src}: {e}")
return None
def create_custom_styles():
"""Create custom paragraph styles for different markdown elements"""
styles = getSampleStyleSheet()
# Title style
styles.add(ParagraphStyle(
name='CustomTitle',
parent=styles['Heading1'],
fontSize=28,
textColor=HexColor('#1e3a8a'),
spaceAfter=12,
alignment=TA_CENTER,
fontName='Helvetica-Bold'
))
# Subtitle style
styles.add(ParagraphStyle(
name='CustomSubtitle',
parent=styles['Heading2'],
fontSize=16,
textColor=HexColor('#1e3a8a'),
spaceAfter=20,
alignment=TA_CENTER,
fontName='Helvetica-Bold'
))
# Metadata badge style
styles.add(ParagraphStyle(
name='MetadataBadge',
parent=styles['BodyText'],
fontSize=11,
textColor=HexColor('#1e40af'),
alignment=TA_CENTER,
spaceAfter=8,
fontName='Helvetica-Bold'
))
# TOC Title
styles.add(ParagraphStyle(
name='TOCTitle',
parent=styles['Heading1'],
fontSize=18,
textColor=HexColor('#1e3a8a'),
spaceAfter=16,
alignment=TA_LEFT,
fontName='Helvetica-Bold'
))
# TOC Entry
styles.add(ParagraphStyle(
name='TOCEntry',
parent=styles['BodyText'],
fontSize=11,
textColor=HexColor('#1f2937'),
spaceAfter=6,
leftIndent=20,
fontName='Helvetica'
))
# Heading styles
styles.add(ParagraphStyle(
name='CustomH1',
parent=styles['Heading1'],
fontSize=20,
textColor=HexColor('#1e3a8a'),
spaceAfter=14,
spaceBefore=20,
fontName='Helvetica-Bold',
keepWithNext=True,
borderWidth=2,
borderColor=HexColor('#3b82f6'),
borderPadding=8,
backColor=HexColor('#eff6ff')
))
styles.add(ParagraphStyle(
name='CustomH2',
parent=styles['Heading2'],
fontSize=16,
textColor=HexColor('#1e40af'),
spaceAfter=10,
spaceBefore=16,
fontName='Helvetica-Bold',
borderWidth=0,
borderColor=None,
borderPadding=0,
keepWithNext=True,
leftIndent=0
))
styles.add(ParagraphStyle(
name='CustomH3',
parent=styles['Heading3'],
fontSize=14,
textColor=HexColor('#2563eb'),
spaceAfter=8,
spaceBefore=12,
fontName='Helvetica-Bold',
keepWithNext=True
))
styles.add(ParagraphStyle(
name='CustomH4',
parent=styles['Heading3'],
fontSize=12,
textColor=HexColor('#3b82f6'),
spaceAfter=6,
spaceBefore=10,
fontName='Helvetica-Bold',
keepWithNext=True
))
# Body text
styles.add(ParagraphStyle(
name='CustomBody',
parent=styles['BodyText'],
fontSize=10.5,
leading=15,
alignment=TA_JUSTIFY,
spaceAfter=10,
textColor=HexColor('#374151')
))
# Code block
styles.add(ParagraphStyle(
name='CustomCode',
parent=styles['Code'],
fontSize=9,
fontName='Courier',
textColor=HexColor('#1f2937'),
backColor=HexColor('#f3f4f6'),
borderWidth=1,
borderColor=HexColor('#d1d5db'),
borderPadding=10,
leftIndent=10,
rightIndent=10,
spaceAfter=12
))
# Blockquote
styles.add(ParagraphStyle(
name='CustomBlockquote',
parent=styles['BodyText'],
fontSize=10,
textColor=HexColor('#4b5563'),
backColor=HexColor('#f9fafb'),
borderWidth=0,
leftIndent=25,
rightIndent=20,
borderPadding=12,
spaceAfter=12,
spaceBefore=12,
fontName='Helvetica-Oblique'
))
# Meta info
styles.add(ParagraphStyle(
name='CustomMeta',
parent=styles['BodyText'],
fontSize=10,
textColor=HexColor('#6b7280'),
alignment=TA_CENTER,
spaceAfter=6
))
# List item
styles.add(ParagraphStyle(
name='CustomListItem',
parent=styles['BodyText'],
fontSize=10.5,
leading=15,
leftIndent=20,
spaceAfter=5,
textColor=HexColor('#374151')
))
# Highlight box (for key investment highlights, recommendations, etc.)
styles.add(ParagraphStyle(
name='HighlightBox',
parent=styles['BodyText'],
fontSize=11,
textColor=HexColor('#1e40af'),
backColor=HexColor('#eff6ff'),
borderWidth=2,
borderColor=HexColor('#3b82f6'),
borderPadding=12,
spaceAfter=14,
spaceBefore=14,
fontName='Helvetica-Bold',
alignment=TA_CENTER
))
styles.add(ParagraphStyle(
name='TableHeader',
parent=styles['BodyText'],
fontSize=10,
fontName='Helvetica-Bold',
textColor=HexColor('#ffffff'),
alignment=TA_CENTER,
))
return styles
def parse_html_to_flowables(html_content, styles, base_url=None, page_w=letter[0], margins=(0.75*inch, 0.75*inch)):
"""
Parse HTML content and convert to ReportLab flowables.
Uses controlled traversal to handle block-level elements properly.
"""
soup = BeautifulSoup(html_content, 'html.parser')
body = soup.body or soup
flowables = []
left_margin, right_margin = margins
# Block-level elements we handle
BLOCKS = {'h1', 'h2', 'h3', 'h4', 'p', 'ul', 'ol', 'table', 'pre', 'blockquote', 'hr', 'img'}
h1_seen = False
for el in body.children:
# Skip non-element nodes and non-block elements
if getattr(el, "name", None) not in BLOCKS:
continue
name = el.name
if name == 'h1':
# Add page break before subsequent H1s for better structure
if h1_seen:
flowables.append(PageBreak())
h1_seen = True
text = to_paragraph_html(el)
if text.strip():
# Create bookmark for h1
anchor_id = create_anchor_id(el.get_text())
flowables.append(BookmarkableHeading(text, styles['CustomH1'], bookmark_name=anchor_id))
flowables.append(Spacer(1, 0.15 * inch))
elif name == 'h2':
text = to_paragraph_html(el)
if text.strip():
# Create bookmark for h2
anchor_id = create_anchor_id(el.get_text())
flowables.append(Spacer(1, 0.1 * inch))
flowables.append(BookmarkableHeading(text, styles['CustomH2'], bookmark_name=anchor_id))
flowables.append(Spacer(1, 0.1 * inch))
elif name == 'h3':
text = to_paragraph_html(el)
if text.strip():
# Create bookmark for h3
anchor_id = create_anchor_id(el.get_text())
flowables.append(BookmarkableHeading(text, styles['CustomH3'], bookmark_name=anchor_id))
flowables.append(Spacer(1, 0.08 * inch))
elif name == 'h4':
text = to_paragraph_html(el)
if text.strip():
# Create bookmark for h4
anchor_id = create_anchor_id(el.get_text())
flowables.append(BookmarkableHeading(text, styles['CustomH4'], bookmark_name=anchor_id))
flowables.append(Spacer(1, 0.06 * inch))
elif name == 'p':
para_html = to_paragraph_html(el)
if para_html.strip():
# Check if this is a highlighted paragraph (starts with **)
raw_text = el.get_text().strip()
# Check if paragraph contains internal links
has_internal_links = '<ilink dest=' in para_html
if raw_text.startswith('**') and raw_text.endswith('**'):
# This is a bold/important statement - use highlight style
if has_internal_links:
flowables.append(InternalLinkParagraph(para_html, styles['HighlightBox']))
else:
flowables.append(Paragraph(para_html, styles['HighlightBox']))
else:
if has_internal_links:
flowables.append(InternalLinkParagraph(para_html, styles['CustomBody']))
else:
flowables.append(Paragraph(para_html, styles['CustomBody']))
flowables.append(Spacer(1, 0.1 * inch))
elif name in ('ul', 'ol'):
list_flowable = build_list(el, styles)
flowables.append(list_flowable)
flowables.append(Spacer(1, 0.12 * inch))
elif name == 'table':
table = build_table(el, styles, page_w, left_margin, right_margin)
if table:
flowables.append(Spacer(1, 0.08 * inch))
flowables.append(table)
flowables.append(Spacer(1, 0.18 * inch))
elif name == 'pre':
# Use XPreformatted for page-breakable code blocks
code = el.get_text()
if code.strip():
flowables.append(XPreformatted(code, styles['CustomCode'], dedent=0))
flowables.append(Spacer(1, 0.12 * inch))
elif name == 'blockquote':
text = to_paragraph_html(el)
if text.strip():
flowables.append(Paragraph(text, styles['CustomBlockquote']))
flowables.append(Spacer(1, 0.12 * inch))
elif name == 'hr':
# Add a horizontal rule for section separation
flowables.append(Spacer(1, 0.15 * inch))
flowables.append(HRFlowable(
width="100%",
thickness=1,
color=HexColor('#cbd5e1'),
spaceBefore=6,
spaceAfter=6
))
flowables.append(Spacer(1, 0.15 * inch))
elif name == 'img':
img = build_image(el, base_url, page_w - left_margin - right_margin)
if img:
flowables.append(img)
flowables.append(Spacer(1, 0.12 * inch))
return flowables
def add_internal_links_to_pdf(pdf_bytes, pending_links, bookmark_registry):
"""
Post-process the generated PDF to add clickable internal links.
Uses pypdf to add link annotations that point to bookmarks.
"""
if not pending_links or not bookmark_registry:
return pdf_bytes
try:
# Read the PDF
pdf_reader = PdfReader(BytesIO(pdf_bytes))
pdf_writer = PdfWriter()
# Copy all pages
for page in pdf_reader.pages:
pdf_writer.add_page(page)
# Add link annotations for pending links
for link_info in pending_links:
page_num = link_info['page'] - 1 # Convert to 0-indexed
rect = link_info['rect']
dest_name = link_info['dest']
# Check if the destination bookmark exists
if dest_name in bookmark_registry:
dest_info = bookmark_registry[dest_name]
dest_page = dest_info['page'] - 1 # Convert to 0-indexed
# Create a link annotation
# This would require manual PDF annotation creation which is complex
# For now, we'll skip this and rely on PDF bookmarks which ARE clickable
pass
# Write to bytes
output_buffer = BytesIO()
pdf_writer.write(output_buffer)
return output_buffer.getvalue()
except Exception as e:
print(f"Warning: Could not post-process PDF links: {e}")
# Return original PDF if post-processing fails
return pdf_bytes
def convert_md_to_pdf(md_content: str, ticker: str, *, base_url: str | None = None) -> bytes:
"""
Convert Markdown to a styled, structurally faithful PDF using ReportLab.
Args:
md_content: Markdown content to convert
ticker: Stock ticker symbol
base_url: Base URL for resolving relative paths (images, links)
Returns:
PDF file as bytes
"""
try:
# Extract metadata from the first few lines
lines = md_content.split('\n')
company_name = lines[0].lstrip('#').strip() if lines else ticker
subtitle = lines[1].lstrip('#').strip() if len(lines) > 1 else "Investment Analysis Report"
# Extract report date, sector, industry, exchange
metadata = {}
for line in lines[:10]:
if '**Report Date**:' in line:
metadata['date'] = line.split(':', 1)[1].strip()
elif '**Sector**:' in line:
parts = line.split('|')
if len(parts) >= 2:
metadata['sector'] = parts[0].split(':', 1)[1].strip()
metadata['industry'] = parts[1].split(':', 1)[1].strip() if ':' in parts[1] else parts[1].strip()
elif '**Exchange**:' in line:
metadata['exchange'] = line.split(':', 1)[1].strip()
# 1) Convert Markdown to HTML
html_content = markdown.markdown(
md_content,
output_format="html5",
extensions=[
"extra",
"sane_lists",
"nl2br",
],
)
# Extract TOC sections from HTML
soup = BeautifulSoup(html_content, 'html.parser')
h2_headings = []
for h2 in soup.find_all('h2'):
heading_text = h2.get_text().strip()
# Skip the "Table of Contents" heading itself
if heading_text and heading_text != "Table of Contents":
h2_headings.append(heading_text)
# 2) Create PDF
buffer = BytesIO()
doc = SimpleDocTemplate(
buffer,
pagesize=letter,
rightMargin=0.75*inch,
leftMargin=0.75*inch,
topMargin=0.75*inch,
bottomMargin=0.75*inch,
)
# 3) Create custom styles
styles = create_custom_styles()
# 4) Build content
story = []
# ===== COVER PAGE =====
story.append(Spacer(1, 1.5*inch))
# Company name with decorative line
story.append(HRFlowable(
width="60%",
thickness=2,
color=HexColor('#3b82f6'),
spaceBefore=0,
spaceAfter=12
))
story.append(Paragraph(company_name, styles['CustomTitle']))
story.append(HRFlowable(
width="60%",
thickness=2,
color=HexColor('#3b82f6'),
spaceBefore=12,
spaceAfter=24
))
# Subtitle
story.append(Paragraph(subtitle, styles['CustomSubtitle']))
story.append(Spacer(1, 0.4*inch))
# Metadata badges
if metadata.get('sector'):
story.append(Paragraph(
f"<b>Sector:</b> {metadata['sector']}",
styles['MetadataBadge']
))
if metadata.get('industry'):
story.append(Paragraph(
f"<b>Industry:</b> {metadata['industry']}",
styles['MetadataBadge']
))
if metadata.get('exchange'):
story.append(Paragraph(
f"<b>Exchange:</b> {metadata['exchange']}",
styles['MetadataBadge']
))
story.append(Spacer(1, 0.8*inch))
# Report generation info
report_date = metadata.get('date', datetime.now().strftime('%B %d, %Y'))
story.append(Paragraph(
f"Report Date: {report_date}",
styles['CustomMeta']
))
story.append(Paragraph(
f"Generated: {datetime.now().strftime('%B %d, %Y at %I:%M %p')}",
styles['CustomMeta']
))
story.append(PageBreak())
# ===== TABLE OF CONTENTS =====
if h2_headings:
story.append(Spacer(1, 0.5*inch))
story.append(Paragraph("Table of Contents", styles['TOCTitle']))
story.append(Spacer(1, 0.2*inch))
for idx, heading in enumerate(h2_headings, 1):
# Create anchor ID for the heading
anchor_id = create_anchor_id(heading)
# Create clickable TOC entry with internal link using our custom ilink tag
toc_entry = f'{idx}. <ilink dest="{anchor_id}">{heading}</ilink>'
story.append(InternalLinkParagraph(toc_entry, styles['TOCEntry']))
story.append(Spacer(1, 0.3*inch))
story.append(HRFlowable(
width="100%",
thickness=1,
color=HexColor('#cbd5e1'),
spaceBefore=6,
spaceAfter=6
))
story.append(PageBreak())
# 5) Parse HTML and add to story
flowables = parse_html_to_flowables(
html_content,
styles,
base_url=base_url,
page_w=letter[0],
margins=(doc.leftMargin, doc.rightMargin)
)
story.extend(flowables)
# 6) Build PDF
# Clear pending links before building
global _pending_links, _bookmark_registry
_pending_links = []
_bookmark_registry = {}
doc.build(story, onFirstPage=footer, onLaterPages=footer)
# 7) Post-process PDF to add clickable internal links
pdf_bytes = buffer.getvalue()
buffer.close()
# Add internal links using pypdf
pdf_bytes = add_internal_links_to_pdf(pdf_bytes, _pending_links, _bookmark_registry)
return pdf_bytes
except Exception as e:
raise HTTPException(status_code=500, detail=f"PDF generation failed: {str(e)}")