ABDALLALSWAITI commited on
Commit
19596d2
Β·
verified Β·
1 Parent(s): 5ec1f6e

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +49 -15
src/streamlit_app.py CHANGED
@@ -79,23 +79,43 @@ def save_uploaded_images(images, temp_dir):
79
 
80
  # Create mapping
81
  image_mapping[image.name] = f"images/{image.name}"
 
82
 
83
  return image_mapping
84
 
85
- def process_html_with_images(html_content, image_mapping):
86
- """Process HTML to handle image references"""
87
- for original_name, new_path in image_mapping.items():
88
- # Handle various image reference patterns
89
- patterns = [
90
- (f'src="{original_name}"', f'src="{new_path}"'),
91
- (f"src='{original_name}'", f"src='{new_path}'"),
92
- (f'href="{original_name}"', f'href="{new_path}"'),
93
- (f"href='{original_name}'", f"href='{new_path}'"),
94
- ]
 
 
 
 
 
 
 
95
 
96
- for old_pattern, new_pattern in patterns:
97
- if old_pattern in html_content:
98
- html_content = html_content.replace(old_pattern, new_pattern)
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
  return html_content
101
 
@@ -380,8 +400,15 @@ with tab1:
380
  # Process images if uploaded
381
  if uploaded_images:
382
  image_mapping = save_uploaded_images(uploaded_images, temp_dir)
383
- html_content = process_html_with_images(html_content, image_mapping)
384
  st.info(f"πŸ“· Processed {len(uploaded_images)} image(s)")
 
 
 
 
 
 
 
385
 
386
  # Convert to PDF
387
  pdf_bytes, error = convert_html_to_pdf(html_content, aspect_ratio_file, temp_dir)
@@ -527,8 +554,15 @@ with tab2:
527
  processed_html = html_code
528
  if uploaded_images_text:
529
  image_mapping = save_uploaded_images(uploaded_images_text, temp_dir)
530
- processed_html = process_html_with_images(html_code, image_mapping)
531
  st.info(f"πŸ“· Processed {len(uploaded_images_text)} image(s)")
 
 
 
 
 
 
 
532
 
533
  # Convert to PDF
534
  pdf_bytes, error = convert_html_to_pdf(processed_html, aspect_ratio_text, temp_dir)
 
79
 
80
  # Create mapping
81
  image_mapping[image.name] = f"images/{image.name}"
82
+ print(f"Saved image: {image.name} -> {image_path}")
83
 
84
  return image_mapping
85
 
86
+ def process_html_with_images(html_content, temp_dir, image_mapping):
87
+ """Process HTML to handle image references with absolute file paths"""
88
+ import re
89
+
90
+ for original_name, relative_path in image_mapping.items():
91
+ # Get absolute path for the image
92
+ absolute_path = os.path.abspath(os.path.join(temp_dir, relative_path))
93
+ file_url = f"file://{absolute_path}"
94
+
95
+ # Replace various image reference patterns
96
+ # Pattern 1: src="filename" or src='filename'
97
+ html_content = re.sub(
98
+ f'src=["\'](?:\.\/)?{re.escape(original_name)}["\']',
99
+ f'src="{file_url}"',
100
+ html_content,
101
+ flags=re.IGNORECASE
102
+ )
103
 
104
+ # Pattern 2: background-image: url(filename)
105
+ html_content = re.sub(
106
+ f'url\(["\']?(?:\.\/)?{re.escape(original_name)}["\']?\)',
107
+ f'url("{file_url}")',
108
+ html_content,
109
+ flags=re.IGNORECASE
110
+ )
111
+
112
+ # Pattern 3: href for links
113
+ html_content = re.sub(
114
+ f'href=["\'](?:\.\/)?{re.escape(original_name)}["\']',
115
+ f'href="{file_url}"',
116
+ html_content,
117
+ flags=re.IGNORECASE
118
+ )
119
 
120
  return html_content
121
 
 
400
  # Process images if uploaded
401
  if uploaded_images:
402
  image_mapping = save_uploaded_images(uploaded_images, temp_dir)
403
+ html_content = process_html_with_images(html_content, temp_dir, image_mapping)
404
  st.info(f"πŸ“· Processed {len(uploaded_images)} image(s)")
405
+ # Debug info
406
+ with st.expander("πŸ” Debug: Image Mapping"):
407
+ for orig, new in image_mapping.items():
408
+ st.text(f"{orig} -> {new}")
409
+ full_path = os.path.join(temp_dir, new)
410
+ st.text(f"Full path: {full_path}")
411
+ st.text(f"Exists: {os.path.exists(full_path)}")
412
 
413
  # Convert to PDF
414
  pdf_bytes, error = convert_html_to_pdf(html_content, aspect_ratio_file, temp_dir)
 
554
  processed_html = html_code
555
  if uploaded_images_text:
556
  image_mapping = save_uploaded_images(uploaded_images_text, temp_dir)
557
+ processed_html = process_html_with_images(html_code, temp_dir, image_mapping)
558
  st.info(f"πŸ“· Processed {len(uploaded_images_text)} image(s)")
559
+ # Debug info
560
+ with st.expander("πŸ” Debug: Image Mapping"):
561
+ for orig, new in image_mapping.items():
562
+ st.text(f"{orig} -> {new}")
563
+ full_path = os.path.join(temp_dir, new)
564
+ st.text(f"Full path: {full_path}")
565
+ st.text(f"Exists: {os.path.exists(full_path)}")
566
 
567
  # Convert to PDF
568
  pdf_bytes, error = convert_html_to_pdf(processed_html, aspect_ratio_text, temp_dir)