-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
204 lines (181 loc) · 7.28 KB
/
Copy pathapp.py
File metadata and controls
204 lines (181 loc) · 7.28 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
import io
import sys
import os
import requests
import streamlit as st
import fitz # pymupdf
sys.path.insert(0, os.path.dirname(__file__))
from pdf_notebook import PDFHyperlinkedNotebookGenerator
st.set_page_config(page_title="Notebook Generator", page_icon="📓", layout="centered")
st.title("📓 PDF Notebook Generator")
st.caption("Generate custom notebooks for reMarkable, Onyx Boox, Supernote, Kindle Scribe, and standard paper sizes.")
st.markdown(
"""
Each notebook starts with a **title page** followed by a **table of contents**, then your chosen number of pages.
Every entry in the table of contents links directly to its corresponding page in the notebook,
and every page number links back to the table of contents.
Because the links are page-based rather than bookmark-based, **inserting pages inside the notebook
preserves all navigation**. The links in the TOC will link to the page embedded in the document,
even if you add pages.
The page styles include:
- Dot grid
- Lines
- Grid
- Blank
""")
# --- Device options ---
DEVICES = {
"reMarkable": {
"reMarkable 2": "remarkable2",
"reMarkable 1": "remarkable1",
"reMarkable Paper Pro": "remarkablepro",
"reMarkable Paper Pro Move":"remarkablemove",
},
"Onyx Boox": {
"Note Air": "booxnoteair",
"Note Air 3": "booxnoteair3",
"Note Air 3C": "booxnoteair3c",
"Note Air 4C": "booxnoteair4c",
"Note Air 5C": "booxnoteair5c",
"Note Max": "booxnotemax",
"Max Lumi": "booxmaxlumi",
"Tab Mini C": "booxtabminic",
"Tab Ultra C Pro": "booxtabultracpro",
"Tab X": "booxtabx",
"Tab X C": "booxtabxc",
"Go 6": "booxgo6",
"Go 7": "booxgo7",
"Go 10.3": "booxgo103",
"Palma 2": "booxpalma2",
},
"Kindle": {
"Scribe": "kindlescribe",
},
"Supernote": {
"A5X": "supernotea5x",
"A6X": "supernotea6x",
"A6X2 (Nomad)": "supernotea6x2",
"A5X2 (Manta)": "supernotemanta",
},
"Standard Paper": {
"A4": "a4",
"A5": "a5",
"Letter": "letter",
"Legal": "legal",
},
}
PATTERNS = {
"Dots": "dots",
"Lines": "lines",
"Grid": "grid",
"Blank": "blank",
}
PAGE_NUMBER_POSITIONS = {
"Lower Left": "lower-left",
"Lower Right": "lower-right",
"Lower Middle": "lower-middle",
"Upper Right": "upper-right",
"Upper Middle": "upper-middle",
"None": None,
}
# --- Layout ---
col1, col2 = st.columns(2)
with col1:
device_group = st.selectbox("Device type", list(DEVICES.keys()))
device_display = st.selectbox("Device", list(DEVICES[device_group].keys()))
device_key = DEVICES[device_group][device_display]
pattern_display = st.selectbox("Page pattern", list(PATTERNS.keys()))
pattern_key = PATTERNS[pattern_display]
num_pages = st.number_input("Number of pages", min_value=1, max_value=1000, value=100, step=10)
with col2:
spacing_mm = st.number_input("Spacing (mm)", min_value=2.0, max_value=20.0, value=5.0, step=0.5,
help="Spacing between dots, lines, or grid cells")
pos_display = st.selectbox("Page number position", list(PAGE_NUMBER_POSITIONS.keys()))
page_number_position = PAGE_NUMBER_POSITIONS[pos_display]
include_title_page = st.checkbox("Include title page", value=True)
include_toc = st.checkbox("Include table of contents", value=True)
with st.expander("Margins (mm)"):
mc1, mc2 = st.columns(2)
with mc1:
margin_left = st.number_input("Left", min_value=0, max_value=50, value=5)
margin_top = st.number_input("Top", min_value=0, max_value=50, value=5)
with mc2:
margin_right = st.number_input("Right", min_value=0, max_value=50, value=5)
margin_bottom = st.number_input("Bottom", min_value=0, max_value=50, value=5)
st.divider()
if st.button("Generate PDF", type="primary", use_container_width=True):
with st.spinner("Generating your notebook..."):
try:
buffer = io.BytesIO()
generator = PDFHyperlinkedNotebookGenerator(
filename=buffer,
num_pages=num_pages,
page_size=device_key,
page_pattern=pattern_key,
spacing_mm=spacing_mm,
page_number_position=page_number_position,
include_title_page=include_title_page,
include_toc=include_toc,
margins={
"left": margin_left,
"right": margin_right,
"top": margin_top,
"bottom": margin_bottom,
},
)
num_toc_pages = generator._calculate_toc_pages() if include_toc else 0
title_pages = 1 if include_title_page else 0
first_content_idx = title_pages + num_toc_pages
generator.generate()
buffer.seek(0)
pdf_bytes = buffer.read()
filename = f"{device_display} - {pattern_display} - {num_pages}p.pdf"
st.success("Notebook generated!")
st.download_button(
label="Download PDF",
data=pdf_bytes,
file_name=filename,
mime="application/pdf",
use_container_width=True,
)
doc = fitz.open(stream=pdf_bytes, filetype="pdf")
preview_indices = []
preview_labels = []
if include_title_page:
preview_indices.append(0)
preview_labels.append("Title page")
if include_toc:
preview_indices.append(title_pages)
preview_labels.append("Table of contents")
if first_content_idx < len(doc):
preview_indices.append(first_content_idx)
preview_labels.append("Content page")
if preview_indices:
st.markdown("**Preview:**")
cols = st.columns(len(preview_indices))
for col, idx, label in zip(cols, preview_indices, preview_labels):
pix = doc[idx].get_pixmap(matrix=fitz.Matrix(1.5, 1.5))
col.image(pix.tobytes("png"), use_container_width=True)
col.caption(label)
except Exception as e:
st.error(f"Error generating notebook: {e}")
st.divider()
st.subheader("Feedback")
with st.form("contact_form"):
name = st.text_input("Name")
email = st.text_input("Email (optional, if you'd like a reply)")
message = st.text_area("Message")
submitted = st.form_submit_button("Send", use_container_width=True)
if submitted:
if not message.strip():
st.warning("Please enter a message.")
else:
response = requests.post(
"https://formspree.io/f/xaqdzpjw",
data={"name": name, "email": email, "message": message},
headers={"Referer": "https://eink-notebook-templates.streamlit.app"},
)
if response.status_code == 200:
st.success("Thanks for your feedback!")
else:
st.error("Something went wrong. Please try again.")