faizhalas commited on
Commit
d4ec5e7
·
verified ·
1 Parent(s): 1fd64e3

Update pages/reader.py

Browse files
Files changed (1) hide show
  1. pages/reader.py +17 -97
pages/reader.py CHANGED
@@ -2,11 +2,7 @@
2
  import streamlit as st
3
  import pandas as pd
4
  import html
5
- from reportlab.platypus import (SimpleDocTemplate, Paragraph, Spacer, Frame, PageTemplate, NextPageTemplate, PageBreak)
6
- from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
7
- from reportlab.lib.pagesizes import A4
8
- from reportlab.lib.units import cm
9
- from reportlab.lib.enums import TA_CENTER, TA_JUSTIFY
10
  import io
11
 
12
 
@@ -33,91 +29,13 @@ df = connect_gsheet()
33
  def format_html(text):
34
  return html.escape(text).replace("\n", "<br>")
35
 
36
- def create_pdf():
37
- buffer = io.BytesIO()
38
-
39
- doc = SimpleDocTemplate(
40
- buffer, pagesize=A4,
41
- rightMargin=2*cm, leftMargin=2*cm,
42
- topMargin=2*cm, bottomMargin=2*cm
43
- )
44
-
45
- # Styles
46
- styles = getSampleStyleSheet()
47
- styles.add(ParagraphStyle(
48
- name="ArticleTitle", alignment=TA_CENTER,
49
- fontSize=28, leading=32, spaceAfter=6,
50
- textColor="#222", fontName="Times-Bold"
51
- ))
52
- styles.add(ParagraphStyle(
53
- name="Author", alignment=TA_CENTER,
54
- fontSize=14, leading=18, textColor="#666",
55
- italic=True, spaceAfter=12
56
- ))
57
- styles.add(ParagraphStyle(
58
- name="SectionTitle", alignment=TA_CENTER,
59
- fontSize=18, leading=22, spaceBefore=16, spaceAfter=8,
60
- textColor="#004488", fontName="Times-Bold"
61
- ))
62
- styles.add(ParagraphStyle(
63
- name="Meta", alignment=TA_CENTER,
64
- fontSize=fontsize, leading=18, italic=True,
65
- textColor="#333", spaceAfter=10
66
- ))
67
- styles.add(ParagraphStyle(
68
- name="Content", alignment=TA_JUSTIFY,
69
- fontSize=fontsize, leading=18, textColor="#333"
70
- ))
71
-
72
- # Page templates
73
- frame_full = Frame(2*cm, 2*cm, A4[0]-4*cm, A4[1]-4*cm, id="normal")
74
- frame_col1 = Frame(2*cm, 2*cm, (A4[0]-4*cm)/2 - 0.5*cm, A4[1]-4*cm, id="col1")
75
- frame_col2 = Frame(2*cm + (A4[0]-4*cm)/2 + 0.5*cm, 2*cm,
76
- (A4[0]-4*cm)/2 - 0.5*cm, A4[1]-4*cm, id="col2")
77
-
78
- template_single = PageTemplate(id="OneCol", frames=[frame_full])
79
- template_double = PageTemplate(id="TwoCol", frames=[frame_col1, frame_col2])
80
-
81
- doc.addPageTemplates([template_double, template_single])
82
-
83
- # Elements
84
- elements = []
85
-
86
- # --- Cover (single column) ---
87
- elements.append(Paragraph(title, styles["ArticleTitle"]))
88
- elements.append(Paragraph(f"By {author}", styles["Author"]))
89
- elements.append(Paragraph(affiliation, styles["Author"]))
90
- elements.append(Spacer(1, 12))
91
-
92
- elements.append(Paragraph("Abstract", styles["SectionTitle"]))
93
- elements.append(Paragraph(abstracten, styles["Meta"]))
94
- elements.append(Spacer(1, 12))
95
-
96
- elements.append(Paragraph("Abstrak", styles["SectionTitle"]))
97
- elements.append(Paragraph(abstractid, styles["Meta"]))
98
- elements.append(Paragraph(f"Keywords: {keywords}", styles["Author"]))
99
-
100
- # Switch to 2-column template from next page
101
- elements.append(NextPageTemplate("TwoCol"))
102
- elements.append(PageBreak())
103
-
104
- # --- Main content (2 columns) ---
105
- sections = [
106
- ("Introduction", intro),
107
- ("Method", method),
108
- ("Result", result),
109
- ("Discussion", discussion),
110
- ("Result & Discussion", rnd),
111
- ("Conclusion", conclusion),
112
- ]
113
-
114
- for sec_title, sec_content in sections:
115
- elements.append(Paragraph(sec_title, styles["SectionTitle"]))
116
- elements.append(Paragraph(sec_content, styles["Content"]))
117
-
118
- doc.build(elements)
119
- buffer.seek(0)
120
- return buffer
121
 
122
 
123
 
@@ -160,13 +78,7 @@ if selection == "Baca Artikel":
160
  fontsize = st.number_input("Ukuran kata", min_value=5, value=16, step=1)
161
  no_columns = st.number_input("Jumlah kolom", min_value=1, max_value=4, value=1, step=1)
162
 
163
- pdf_buffer = create_pdf()
164
- st.download_button(
165
- label="📄 Download dalam PDF",
166
- data=pdf_buffer,
167
- file_name="article.pdf",
168
- mime="application/pdf"
169
- )
170
 
171
  if not filtered_df.empty:
172
 
@@ -245,6 +157,14 @@ if selection == "Baca Artikel":
245
  """
246
 
247
  st.markdown(html_output, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
248
 
249
  else:
250
  st.warning("No article found with the specified ID.")
 
2
  import streamlit as st
3
  import pandas as pd
4
  import html
5
+ from xhtml2pdf import pisa
 
 
 
 
6
  import io
7
 
8
 
 
29
  def format_html(text):
30
  return html.escape(text).replace("\n", "<br>")
31
 
32
+ def html_to_pdf(html_content: str) -> bytes:
33
+ result = io.BytesIO()
34
+ pisa_status = pisa.CreatePDF(io.StringIO(html_content), dest=result)
35
+ if pisa_status.err:
36
+ return None
37
+ result.seek(0)
38
+ return result.read()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
 
41
 
 
78
  fontsize = st.number_input("Ukuran kata", min_value=5, value=16, step=1)
79
  no_columns = st.number_input("Jumlah kolom", min_value=1, max_value=4, value=1, step=1)
80
 
81
+
 
 
 
 
 
 
82
 
83
  if not filtered_df.empty:
84
 
 
157
  """
158
 
159
  st.markdown(html_output, unsafe_allow_html=True)
160
+
161
+ pdf_bytes = html_to_pdf(html_output)
162
+ st.download_button(
163
+ "📄 Download PDF",
164
+ data=pdf_bytes,
165
+ file_name="article.pdf",
166
+ mime="application/pdf"
167
+ )
168
 
169
  else:
170
  st.warning("No article found with the specified ID.")