File size: 8,361 Bytes
f56c398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
For this app take current version below and add the ability to generate it not only for each page layout but also every font to paragraph lead caharcter mappings with smart mappings to process markdown bold and markdown outlines and outline characters and alignement including tables and outlines to special font and layout instructions that mimic the markdown beauty with unicode reflect with maximally the right combinations of ofontsso first read the ttf file and determine which fonts then use those dynamically and have output sample of each ready for user:  import streamlit as st

from pathlib import Path

import base64

import datetime

import re

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak

from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle

from reportlab.lib.pagesizes import letter, A4, legal, landscape

from reportlab.lib.units import inch

from reportlab.lib import colors



# --- Configuration & Setup ---



# Define layouts using reportlab's pagesizes

# The 'size' key now holds a tuple (width, height)

LAYOUTS = {

    "A4 Portrait": {"size": A4, "icon": "📄"},

    "A4 Landscape": {"size": landscape(A4), "icon": "📄"},

    "Letter Portrait": {"size": letter, "icon": "📄"},

    "Letter Landscape": {"size": landscape(letter), "icon": "📄"},

    "Legal Portrait": {"size": legal, "icon": "📄"},

    "Legal Landscape": {"size": landscape(legal), "icon": "📄"},

}



# Directory to save the generated PDFs

OUTPUT_DIR = Path("generated_pdfs")

OUTPUT_DIR.mkdir(exist_ok=True)



# --- ReportLab PDF Generation ---



def markdown_to_story(markdown_text: str):

    """Converts a markdown string into a list of ReportLab Flowables (a 'story')."""

    styles = getSampleStyleSheet()

    

    # Define custom styles

    style_normal = styles['BodyText']

    style_h1 = styles['h1']

    style_h2 = styles['h2']

    style_h3 = styles['h3']

    style_code = styles['Code']

    

    # A simple regex-based parser for markdown

    story = []

    lines = markdown_text.split('\n')

    

    in_code_block = False

    code_block_text = ""



    for line in lines:

        if line.strip().startswith("```"):

            if in_code_block:

                story.append(Paragraph(code_block_text.replace('\n', '<br/>'), style_code))

                in_code_block = False

                code_block_text = ""

            else:

                in_code_block = True

            continue



        if in_code_block:

            # Escape HTML tags for code blocks

            escaped_line = line.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')

            code_block_text += escaped_line + '\n'

            continue



        if line.startswith("# "):

            story.append(Paragraph(line[2:], style_h1))

        elif line.startswith("## "):

            story.append(Paragraph(line[3:], style_h2))

        elif line.startswith("### "):

            story.append(Paragraph(line[4:], style_h3))

        elif line.strip().startswith(("* ", "- ")):

            # Handle bullet points

            story.append(Paragraph(f"• {line.strip()[2:]}", style_normal, bulletText='•'))

        elif re.match(r'^\d+\.\s', line.strip()):

            # Handle numbered lists

            story.append(Paragraph(line.strip(), style_normal))

        elif line.strip() == "":

            story.append(Spacer(1, 0.2 * inch))

        else:

            # Handle bold and italics

            line = re.sub(r'\*\*(.*?)\*\*', r'<b>\1</b>', line)

            line = re.sub(r'_(.*?)_', r'<i>\1</i>', line)

            story.append(Paragraph(line, style_normal))

            

    return story



def create_pdf_with_reportlab(md_path: Path, layout_name: str, layout_properties: dict):

    """Creates a PDF for a given markdown file and layout."""

    try:

        md_content = md_path.read_text(encoding="utf-8")

        

        date_str = datetime.datetime.now().strftime("%Y-%m-%d")

        output_filename = f"{md_path.stem}_{layout_name.replace(' ', '-')}_{date_str}.pdf"

        output_path = OUTPUT_DIR / output_filename



        doc = SimpleDocTemplate(

            str(output_path),

            pagesize=layout_properties.get("size", A4),

            rightMargin=inch,

            leftMargin=inch,

            topMargin=inch,

            bottomMargin=inch

        )

        

        story = markdown_to_story(md_content)

        

        doc.build(story)

        

    except Exception as e:

        st.error(f"Failed to process {md_path.name} with ReportLab: {e}")





# --- Streamlit UI and File Handling (Mostly Unchanged) ---



def get_file_download_link(file_path: Path) -> str:

    """Generates a base64-encoded download link for a file."""

    with open(file_path, "rb") as f:

        data = base64.b64encode(f.read()).decode()

    return f'<a href="data:application/octet-stream;base64,{data}" download="{file_path.name}">Download</a>'



def display_file_explorer():

    """Renders a simple file explorer in the Streamlit app."""

    st.header("📂 File Explorer")



    st.subheader("Source Markdown Files (.md)")

    md_files = list(Path(".").glob("*.md"))

    if not md_files:

        st.info("No Markdown files found. Create a `.md` file to begin.")

    else:

        for md_file in md_files:

            col1, col2 = st.columns([0.8, 0.2])

            with col1:

                st.write(f"📝 `{md_file.name}`")

            with col2:

                st.markdown(get_file_download_link(md_file), unsafe_allow_html=True)



    st.subheader("Generated PDF Files")

    pdf_files = sorted(list(OUTPUT_DIR.glob("*.pdf")), key=lambda p: p.stat().st_mtime, reverse=True)

    if not pdf_files:

        st.info("No PDFs generated yet. Click the button above.")

    else:

        for pdf_file in pdf_files:

            col1, col2 = st.columns([0.8, 0.2])

            with col1:

                st.write(f"📄 `{pdf_file.name}`")

            with col2:

                st.markdown(get_file_download_link(pdf_file), unsafe_allow_html=True)





# --- Main App ---



st.set_page_config(layout="wide", page_title="PDF Generator")



st.title("📄 Markdown to PDF Generator (ReportLab Engine)")

st.markdown("This tool finds all `.md` files in this directory, converts them to PDF in various layouts, and provides download links. It uses the `ReportLab` library and requires no external dependencies.")



if not list(Path(".").glob("*.md")):

    with open("sample.md", "w", encoding="utf-8") as f:

        f.write("# Sample Document\n\nThis is a sample markdown file. **ReportLab** is now creating the PDF.\n\n### Features\n- Item 1\n- Item 2\n\n1. Numbered item\n2. Another one\n\n```\ndef hello():\n    print(\"Hello, PDF!\")\n```\n")

    st.rerun()



if st.button("🚀 Generate PDFs from all Markdown Files", type="primary"):

    markdown_files = list(Path(".").glob("*.md"))

    

    if not markdown_files:

        st.warning("No `.md` files found. Please add a markdown file to the directory.")

    else:

        total_pdfs = len(markdown_files) * len(LAYOUTS)

        progress_bar = st.progress(0)

        pdf_count = 0



        with st.spinner("Generating PDFs using ReportLab..."):

            for md_file in markdown_files:

                st.info(f"Processing: **{md_file.name}**")

                for name, properties in LAYOUTS.items():

                    st.write(f"   - Generating `{name}` format...")

                    create_pdf_with_reportlab(md_file, name, properties)

                    pdf_count += 1

                    progress_bar.progress(pdf_count / total_pdfs)



        st.success("✅ PDF generation complete!")

        st.rerun()



display_file_explorer()