-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
188 lines (141 loc) · 4.86 KB
/
main.py
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
import asyncio
import inspect
import json
import os
import sys
from urllib.parse import urljoin, urlparse, urlunparse
import requests
from bs4 import BeautifulSoup
from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import (
Destination,
Fit,
IndirectObject,
NameObject,
NumberObject,
PdfObject,
TextStringObject,
)
from pyppeteer import launch
def download_pages(links, postprocessing=[], html_dir="html/"):
os.makedirs(html_dir, exist_ok=True)
meta = []
for i, url in enumerate(links):
response = requests.get(url)
print(f"Downloading HTML: {i}/{len(links)-1}")
if response.status_code == 200:
soup = BeautifulSoup(response.content, "html.parser")
for func in postprocessing:
func(i, response, soup)
title = (
soup.select_one("title").get_text()
if soup.select_one("title")
else os.path.basename(url)
)
path = os.path.join(html_dir, f"{i}.html")
with open(path, "w") as file:
file.write(soup.prettify())
meta.append({"id": i, "title": title, "url": url})
else:
print(f"Error downloading HTML page (code: {response.status_code} ): {url}")
exit(1)
with open(os.path.join(html_dir, "metadata.json"), "w") as f:
f.write(json.dumps(meta))
async def html_to_pdf(
html_dir="html/", pdf_dir="pdf/", browser_opts={"headless": True}
):
os.makedirs(pdf_dir, exist_ok=True)
with open(os.path.join(html_dir, "metadata.json")) as f:
html_metadata = json.load(f)
print("Starting browser...")
browser = await launch(**browser_opts)
for i, html_m in enumerate(html_metadata):
print(f"Converting HTML to PDF: {i}/{len(html_metadata)-1}")
id = html_m["id"]
html_path = os.path.join(html_dir, f"{id}.html")
pdf_path = os.path.join(pdf_dir, f"{id}.pdf")
page = await browser.newPage()
with open(html_path, "r", encoding="utf-8") as f:
content = f.read()
await page.goto("about:blank")
await page.setContent(content)
await page.waitFor(5000) # wait 5 secs for page to load
await page.pdf(
{
"path": pdf_path,
"format": "A4",
"margin": {
"top": "0.4 in",
"bottom": "0.4 in",
"right": "0.4 in",
"left": "0.4 in",
},
}
)
await page.close()
with open(os.path.join(pdf_dir, "metadata.json"), "w") as f:
f.write(json.dumps(html_metadata))
await browser.close()
def merge_pdf(pdf_dir="pdf/", output_path="output.pdf"):
print("Merging PDFs...")
with open(os.path.join(pdf_dir, "metadata.json")) as f:
pdf_metadata = json.load(f)
writer = PdfWriter()
b = 0
bookmarks = {}
for pdf_m in pdf_metadata:
pdf_path = os.path.join(pdf_dir, f"{pdf_m['id']}.pdf")
reader = PdfReader(pdf_path)
writer.add_outline_item(pdf_m["title"], b)
bookmarks.update(
{
pdf_m["url"]: {
"title": pdf_m["title"],
"page": b,
}
}
)
for page in reader.pages:
writer.add_page(page)
b += 1
# replace url to internal links
for page in writer.pages:
annot: IndirectObject
for annot in page.get("/Annots", []):
annot_obj: PdfObject = annot.get_object()
if "/A" not in annot_obj:
continue
link_obj = annot_obj["/A"]
# Cleanup uri
uri = link_obj.get("/URI", "")
parsed_uri = urlparse(uri)
uri = urlunparse(
(parsed_uri.scheme, parsed_uri.netloc, parsed_uri.path, "", "", "")
)
if uri in bookmarks:
link_obj.pop("/URI")
link_obj[TextStringObject("/S")] = NameObject("/GoTo")
link_obj[TextStringObject("/D")] = Destination(
bookmarks[uri]["title"],
NumberObject(bookmarks[uri]["page"]),
Fit(
"/XYZ",
(None, None, 0),
),
)
writer.write(output_path)
writer.close()
from html_processing import *
from urls import LINKS
if __name__ == "__main__":
html_files = download_pages(
LINKS,
[
obj
for name, obj in inspect.getmembers(sys.modules[__name__])
if (inspect.isfunction(obj) and name.startswith("html_process_"))
],
)
event = asyncio.get_event_loop()
pdf_files = event.run_until_complete(html_to_pdf())
merge_pdf(output_path="composing_programs.pdf")