5
5
from reportlab .lib .pagesizes import A4
6
6
7
7
BASE_URL = "http://docs.nomana-it.fr"
8
+ TEMP_FOLDER = "pdf-temp"
8
9
OUTPUT_FOLDER = "pdf-output"
9
10
COVER_FOLDER = "pdf-cover"
10
- PDF_NAME = "final_documentation .pdf"
11
+ PDF_NAME = "Liberty_User_Guide .pdf"
11
12
COVER_FILE = os .path .join (COVER_FOLDER , "liberty_cover.pdf" )
12
13
13
14
# Hardcoded Navigation Structure
@@ -110,7 +111,7 @@ def generate_toc(pages_with_numbers):
110
111
</body>
111
112
</html>
112
113
"""
113
- toc_file = os .path .join (OUTPUT_FOLDER , "toc.html" )
114
+ toc_file = os .path .join (TEMP_FOLDER , "toc.html" )
114
115
with open (toc_file , "w" ) as file :
115
116
file .write (toc_html )
116
117
print (f"TOC generated as { toc_file } " )
@@ -156,8 +157,9 @@ def add_page_numbers(input_file, output_file):
156
157
print (f"PDF with page numbers saved to { output_file } " )
157
158
158
159
# Generate PDF with Playwright
159
- def generate_pdf_with_cover_and_toc (base_url , pages_with_titles , output_folder ):
160
- os .makedirs (output_folder , exist_ok = True )
160
+ def generate_pdf_with_cover_and_toc (base_url , pages_with_titles ):
161
+ os .makedirs (TEMP_FOLDER , exist_ok = True )
162
+ os .makedirs (OUTPUT_FOLDER , exist_ok = True )
161
163
162
164
with sync_playwright () as p :
163
165
browser = p .chromium .launch ()
@@ -174,7 +176,7 @@ def generate_pdf_with_cover_and_toc(base_url, pages_with_titles, output_folder):
174
176
continue
175
177
176
178
url = f"{ base_url } /{ page_path } "
177
- output_path = os .path .join (output_folder , f"{ page_path .replace ('/' , '_' )} .pdf" )
179
+ output_path = os .path .join (TEMP_FOLDER , f"{ page_path .replace ('/' , '_' )} .pdf" )
178
180
page = context .new_page ()
179
181
page .goto (url , wait_until = "networkidle" )
180
182
handle_cookie_consent (page )
@@ -200,7 +202,7 @@ def generate_pdf_with_cover_and_toc(base_url, pages_with_titles, output_folder):
200
202
201
203
202
204
203
- toc_file = os .path .join (output_folder , "toc.pdf" )
205
+ toc_file = os .path .join (TEMP_FOLDER , "toc.pdf" )
204
206
generate_toc (page_numbers )
205
207
206
208
toc_page = context .new_page ()
@@ -212,12 +214,12 @@ def generate_pdf_with_cover_and_toc(base_url, pages_with_titles, output_folder):
212
214
)
213
215
214
216
215
- merged_output = os .path .join (output_folder , PDF_NAME )
217
+ merged_output = os .path .join (TEMP_FOLDER , PDF_NAME )
216
218
merge_pdfs (pdf_paths , merged_output )
217
219
add_page_numbers (merged_output , merged_output )
218
220
219
221
final_pdf_paths = [COVER_FILE , toc_file , merged_output ]
220
- final_output_path = os .path .join (output_folder , PDF_NAME )
222
+ final_output_path = os .path .join (OUTPUT_FOLDER , PDF_NAME )
221
223
merge_pdfs (final_pdf_paths , final_output_path )
222
224
223
225
browser .close ()
@@ -233,4 +235,4 @@ def merge_pdfs(input_files, output_file):
233
235
234
236
if __name__ == "__main__" :
235
237
pages_with_titles = flatten_nav (NAV )
236
- generate_pdf_with_cover_and_toc (BASE_URL , pages_with_titles , OUTPUT_FOLDER )
238
+ generate_pdf_with_cover_and_toc (BASE_URL , pages_with_titles )
0 commit comments