-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlDocToPdf.py
More file actions
59 lines (48 loc) · 1.67 KB
/
Copy pathHtmlDocToPdf.py
File metadata and controls
59 lines (48 loc) · 1.67 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
#!/usr/bin/python3
import os
import re
import requests
import subprocess
from urllib.parse import urljoin
import sys
import tempfile
import shutil
# Check if any argument is passed, if not print usage and exit
if len(sys.argv) < 2:
print("Requires Dependencies 'sudo apt-get install wkhtmltopdf pdftk'")
print("Usage: python script_name.py <URL>")
print("Example: python script_name.py https://doc.owncloud.com/webui/next/classic_ui/")
sys.exit()
url = sys.argv[1]
urls = []
# Creating a temporary directory
temp_directory = tempfile.mkdtemp()
while url:
response = requests.get(url)
body = response.text
# Add current URL to the list
urls.append(url)
# Step 2: Find next URL
next_url_match = re.search(r'<span class="next"><a href="(.*?)"', body)
next_url = next_url_match.group(1) if next_url_match else None
if next_url:
url = urljoin(url, next_url) # join the base URL with the relative URL
print(url)
else:
url = None # no next URL found, break the loop
# Step 3: Generate PDFs for each URL
pdf_files = []
for i, url in enumerate(urls):
output_filename = os.path.join(temp_directory, f"output_{i}.pdf")
pdf_files.append(output_filename)
subprocess.run(["wkhtmltopdf", url, output_filename])
# Step 4: Merge all PDFs into one
output_pdf = "output.pdf"
counter = 1
while os.path.exists(output_pdf): # check if file already exists
# If exists, change the filename
output_pdf = f"output({counter}).pdf"
counter += 1
subprocess.run(["pdftk", *pdf_files, "cat", "output", output_pdf])
# Optionally, remove individual PDF files and the temporary directory after merging
shutil.rmtree(temp_directory)