Skip to content

Commit c900d3a

Browse files
author
Rodelph
committed
changed the pdfgeneration files
1 parent b0761ac commit c900d3a

4 files changed

Lines changed: 64 additions & 30 deletions

File tree

.gitignore

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
/[Ll]ogs/
1111
/[Uu]ser[Ss]ettings/
1212

13-
/PdfGenerator/__pycache__/
14-
/PdfGenerator/build/
15-
/PdfGenerator/dist/
16-
/PdfGenerator/access.txt
17-
/PdfGenerator/*.spec
18-
/PdfGenerator/*.zip
19-
/PdfGenerator/_ARMarker/*
20-
!/PdfGenerator/_ARMarker/Markers
21-
/PdfGenerator/_ARMarker/Markers/*.meta
22-
/PdfGenerator/_ARMarker/Markers/*.asset
13+
/ARchivist_document_generator/__pycache__/
14+
/ARchivist_document_generator/build/
15+
/ARchivist_document_generator/dist/
16+
/ARchivist_document_generator/access.txt
17+
/ARchivist_document_generator/*.spec
18+
/ARchivist_document_generator/*.zip
19+
/ARchivist_document_generator/_ARMarker/*
20+
!ARchivist_document_generatorr/_ARMarker/Markers
21+
/ARchivist_document_generator/_ARMarker/Markers/*.meta
22+
/ARchivist_document_generator/_ARMarker/Markers/*.asset
2323

2424
# MemoryCaptures can get excessive in size.
2525
# They also could contain extremely sensitive data

ARchivist_document_generator/PDFEngineGit.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@
1515
from github import Github
1616
import json
1717

18+
# Check which platform the program is being executed on
1819
if sys.platform == "win32":
1920
PATH_TO_WKHTMLTOPDF = r'./wkhtmltopdf/bin/wkhtmltopdf.exe'
2021
elif sys.platform in ["linux", "linux2"]:
2122
PATH_TO_WKHTMLTOPDF = r'./wkhtmltopdf/wkhtmltopdf'
2223
elif sys.platform in ["darwin", "os2", "os2emx"]:
2324
PATH_TO_WKHTMLTOPDF = r'./wkhtmltopdf/wkhtmltopdf-mcos'
2425

26+
# Coordinates for where to place the qr code
27+
AP, BP, WID, HEI = 200, 660, 120, 120
28+
29+
# Config setup that looks for the wktml executables within the project
2530
CONFIG = pdfkit.configuration(wkhtmltopdf=PATH_TO_WKHTMLTOPDF)
31+
32+
# Parameter initialization for files and directories
2633
OPT = {
2734
'margin-top': '2in',
2835
'margin-bottom': '1in',
@@ -32,13 +39,17 @@
3239
}
3340
TEMP_PDF_PATH = "./PDF/temp.pdf"
3441
FINAL_PDF_PATH = "./PDF/output.pdf"
42+
43+
# Creating a unique id for the json data
3544
UID = str(uuid.uuid4())
3645

46+
# Returns the number of pages in a PDF file.
3747
def count_pdf_pages(temp_pdf_path):
3848
with open(temp_pdf_path, 'rb') as file:
3949
pdf_reader = PdfReader(file)
4050
return len(pdf_reader.pages)
4151

52+
# Generates QR codes for each page in the PDF.
4253
def generate_qr_codes(num_pages, uid_folder_path):
4354
for p_no in range(num_pages):
4455
text = f'{{"id": "{UID}", "page": {p_no}}}'
@@ -48,12 +59,11 @@ def generate_qr_codes(num_pages, uid_folder_path):
4859
img = qr.make_image(fill='black', back_color='white')
4960
img.save(os.path.join(uid_folder_path, f"{p_no}.png"))
5061

62+
# Embeds QR codes and AR markers into each page of the PDF.
5163
def add_qr_codes_to_pdf(num_pages, temp_pdf_path, final_pdf_path, uid_folder_path):
5264
pdf_reader = PdfReader(temp_pdf_path)
5365
pdf_writer = PdfWriter()
5466

55-
a, b = 200, 660
56-
wid, hei = 120, 120
5767
ar_marker_path = './_ARMarker/Markers/MarkerIcons03.png'
5868

5969
with open(ar_marker_path, 'rb') as marker_file:
@@ -70,8 +80,8 @@ def add_qr_codes_to_pdf(num_pages, temp_pdf_path, final_pdf_path, uid_folder_pat
7080

7181
image_pdf_path = 'image_page.pdf'
7282
c = canvas.Canvas(image_pdf_path, pagesize=letter)
73-
c.drawImage(f"data:image/png;base64,{marker_base64}", a, b, width=wid, height=hei)
74-
c.drawImage(f"data:image/png;base64,{qr_base64}", a + wid + 5, b, width=wid, height=hei)
83+
c.drawImage(f"data:image/png;base64,{marker_base64}", AP, BP, width=WID, height=HEI)
84+
c.drawImage(f"data:image/png;base64,{qr_base64}", AP + WID + 5, BP, width=WID, height=HEI)
7585
c.save()
7686

7787
with open(image_pdf_path, 'rb') as image_pdf_file:
@@ -85,13 +95,12 @@ def add_qr_codes_to_pdf(num_pages, temp_pdf_path, final_pdf_path, uid_folder_pat
8595
with open(final_pdf_path, 'wb') as output_pdf:
8696
pdf_writer.write(output_pdf)
8797

98+
# Extracts hyperlinks from the PDF and updates the JSON bin with metadata.
8899
def process_pdf_metadata(pdf_path, url):
89100
doc = fitz.open(pdf_path)
90-
a, b = 200, 660
91-
wid, hei = 120, 120
92101
json_data = {
93102
'URL': url,
94-
'ar_marker_coordinates': [a, (792 - (b + hei)), (a + wid), (792 - b)],
103+
'ar_marker_coordinates': [AP, (792 - (BP + HEI)), (AP + WID), (792 - BP)],
95104
'pages': []
96105
}
97106

@@ -115,14 +124,15 @@ def process_pdf_metadata(pdf_path, url):
115124

116125
doc.close()
117126

118-
with open('github_access_key.txt', 'r') as file:
127+
with open('access.txt', 'r') as file:
119128
access_token = file.read().strip()
120129
github = Github(access_token)
121130
repo = github.get_repo('seanscofield/archivist')
122131
file_content = json.dumps(json_data)
123132
file_path = f'Assets/CustomAssets/{UID}.json'
124133
repo.create_file(file_path, "Added entry", file_content)
125134

135+
# Converts a URL to a PDF, generates QR codes, embeds them, processes metadata, and opens the final PDF.
126136
def making_pdf_qr(path):
127137
pdfkit.from_url(path, output_path=TEMP_PDF_PATH, configuration=CONFIG, options=OPT, verbose=False)
128138
num_pages = count_pdf_pages(TEMP_PDF_PATH)
@@ -137,6 +147,7 @@ def making_pdf_qr(path):
137147
process_pdf_metadata(FINAL_PDF_PATH, path)
138148
webbrowser.open(FINAL_PDF_PATH)
139149

150+
# Converts a existing HTMLin a local diractory to a PDF, generates QR codes, embeds them, processes metadata, and opens the final PDF.
140151
def process_pdf_file(file_path):
141152
num_pages = count_pdf_pages(file_path)
142153
uid_folder_path = os.path.join("./QR", UID)
@@ -149,6 +160,7 @@ def process_pdf_file(file_path):
149160
process_pdf_metadata(FINAL_PDF_PATH, file_path)
150161
webbrowser.open(FINAL_PDF_PATH)
151162

163+
# GUI Application
152164
class PDFGeneratorApp(QWidget):
153165
def __init__(self):
154166
super().__init__()

ARchivist_document_generator/PDFEngineIO.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os
1+
import os, shutil
22
import sys
33
import pdfkit
44
import base64
@@ -12,14 +12,21 @@
1212
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QLineEdit,
1313
QPushButton, QLabel, QMessageBox, QFileDialog)
1414

15+
# Check which platform the program is being executed on
1516
if sys.platform == "win32":
1617
PATH_TO_WKHTMLTOPDF = r'./wkhtmltopdf/bin/wkhtmltopdf.exe'
1718
elif sys.platform in ["linux", "linux2"]:
1819
PATH_TO_WKHTMLTOPDF = r'./wkhtmltopdf/wkhtmltopdf'
1920
elif sys.platform in ["darwin", "os2", "os2emx"]:
2021
PATH_TO_WKHTMLTOPDF = r'./wkhtmltopdf/wkhtmltopdf-mcos'
2122

23+
# Config setup that looks for the wktml executables within the project
2224
CONFIG = pdfkit.configuration(wkhtmltopdf=PATH_TO_WKHTMLTOPDF)
25+
26+
# Creating a new bin to hold the json on the JSONBIN Database
27+
BIN_ID = create_data()
28+
29+
# Parameter initialization for files and directories
2330
OPT = {
2431
'margin-top': '2in',
2532
'margin-bottom': '1in',
@@ -29,21 +36,25 @@
2936
}
3037
TEMP_PDF_PATH = "./PDF/temp.pdf"
3138
FINAL_PDF_PATH = "./PDF/output.pdf"
32-
BIN_ID = create_data()
3339
QR_FOLDER_PATH = "./QR"
3440
UID_FOLDER_PATH = os.path.join(QR_FOLDER_PATH, BIN_ID)
3541
OUTPUT_FOLDER_PATH = "./PDF"
3642
AR_MARKER_PATH = './_ARMarker/Markers/MarkerIcons03.png'
3743

44+
# Coordinates for where to place the qr code
45+
AP, BP, WID, HEI = 200, 660, 120, 120
46+
3847
# Ensure directories exist
3948
os.makedirs(UID_FOLDER_PATH, exist_ok=True)
4049
os.makedirs(OUTPUT_FOLDER_PATH, exist_ok=True)
4150

51+
# Returns the number of pages in a PDF file.
4252
def count_pdf_pages(temp_pdf_path):
4353
with open(temp_pdf_path, 'rb') as file:
4454
pdf_reader = PdfReader(file)
4555
return len(pdf_reader.pages)
4656

57+
# Generates QR codes for each page in the PDF.
4758
def generate_qr_codes(num_pages):
4859
for p_no in range(num_pages):
4960
text = f'{{"id": "{BIN_ID}", "page": {p_no}}}'
@@ -53,13 +64,11 @@ def generate_qr_codes(num_pages):
5364
img = qr.make_image(fill='black', back_color='white')
5465
img.save(os.path.join(UID_FOLDER_PATH, f"{p_no}.png"))
5566

67+
# Embeds QR codes and AR markers into each page of the PDF.
5668
def add_qr_codes_to_pdf(num_pages, temp_pdf_path, final_pdf_path):
5769
pdf_reader = PdfReader(temp_pdf_path)
5870
pdf_writer = PdfWriter()
5971

60-
a, b = 200, 660
61-
wid, hei = 120, 120
62-
6372
with open(AR_MARKER_PATH, 'rb') as marker_file:
6473
marker_data = marker_file.read()
6574
marker_base64 = base64.b64encode(marker_data).decode('utf-8')
@@ -74,8 +83,8 @@ def add_qr_codes_to_pdf(num_pages, temp_pdf_path, final_pdf_path):
7483

7584
image_pdf_path = 'image_page.pdf'
7685
c = canvas.Canvas(image_pdf_path, pagesize=letter)
77-
c.drawImage(f"data:image/png;base64,{marker_base64}", a, b, width=wid, height=hei)
78-
c.drawImage(f"data:image/png;base64,{qr_base64}", a + wid + 5, b, width=wid, height=hei)
86+
c.drawImage(f"data:image/png;base64,{marker_base64}", AP, BP, width=WID, height=HEI)
87+
c.drawImage(f"data:image/png;base64,{qr_base64}", AP + WID + 5, BP, width=WID, height=HEI)
7988
c.save()
8089

8190
with open(image_pdf_path, 'rb') as image_pdf_file:
@@ -89,20 +98,18 @@ def add_qr_codes_to_pdf(num_pages, temp_pdf_path, final_pdf_path):
8998
with open(final_pdf_path, 'wb') as output_pdf:
9099
pdf_writer.write(output_pdf)
91100

101+
# Extracts hyperlinks from the PDF and updates the JSON bin with metadata.
92102
def process_pdf_metadata(pdf_path, url):
93103
doc = fitz.open(pdf_path)
94-
a, b = 200, 660
95-
wid, hei = 120, 120
96104
json_data = {
97105
'URL': url,
98-
'ar_marker_coordinates': [a, (792 - (b + hei)), (a + wid), (792 - b)],
106+
'ar_marker_coordinates': [AP, (792 - (BP + HEI)), (AP + WID), (792 - BP)],
99107
'pages': []
100108
}
101109

102110
total_pages = doc.page_count
103111
item_count = 0
104112

105-
hyperlink_id = BIN_ID
106113
for page_idx in range(total_pages):
107114
cur_page = doc.load_page(page_idx)
108115
links = cur_page.get_links()
@@ -112,7 +119,7 @@ def process_pdf_metadata(pdf_path, url):
112119
x0, y0, x1, y1 = item['from']
113120
coordinates = [round(coord, 5) for coord in [x0, y0, x1, y1]]
114121
uri = item.get('uri', '')
115-
hyperlink = {'id': f"{hyperlink_id}-{item_count}", 'uri': uri, 'coordinates': coordinates}
122+
hyperlink = {'id': f"{BIN_ID}-{item_count}", 'uri': uri, 'coordinates': coordinates}
116123
hyperlinks.append(hyperlink)
117124
item_count += 1
118125

@@ -121,22 +128,26 @@ def process_pdf_metadata(pdf_path, url):
121128
update_data(bin_id=BIN_ID, json_data=json_data)
122129
doc.close()
123130

131+
# Converts a URL to a PDF, generates QR codes, embeds them, processes metadata, and opens the final PDF.
124132
def making_pdf_qr(path):
125133
pdfkit.from_url(path, output_path=TEMP_PDF_PATH, configuration=CONFIG, options=OPT, verbose=False)
126134
num_pages = count_pdf_pages(TEMP_PDF_PATH)
127135
generate_qr_codes(num_pages)
128136
add_qr_codes_to_pdf(num_pages, TEMP_PDF_PATH, FINAL_PDF_PATH)
129137
os.remove(TEMP_PDF_PATH)
138+
shutil.rmtree(QR_FOLDER_PATH)
130139
process_pdf_metadata(FINAL_PDF_PATH, path)
131140
webbrowser.open(FINAL_PDF_PATH) # Open the final PDF file
132141

142+
# Converts a existing HTMLin a local diractory to a PDF, generates QR codes, embeds them, processes metadata, and opens the final PDF.
133143
def process_pdf_file(file_path):
134144
num_pages = count_pdf_pages(file_path)
135145
generate_qr_codes(num_pages)
136146
add_qr_codes_to_pdf(num_pages, file_path, FINAL_PDF_PATH)
137147
process_pdf_metadata(FINAL_PDF_PATH, file_path)
138148
webbrowser.open(FINAL_PDF_PATH) # Open the final PDF file
139149

150+
# GUI Application
140151
class PDFGeneratorApp(QWidget):
141152
def __init__(self):
142153
super().__init__()

ARchivist_document_generator/jsonio.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import requests
22

3+
# URL: The base URL for JSONBin API.
34
URL = 'https://api.jsonbin.io/v3/b'
45

6+
# HEADERS: Headers required for the API requests, including the Content-Type and the X-Access-Key for authentication.
57
HEADERS = {
68
'Content-Type': 'application/json',
79
'X-Access-Key': '$2a$10$/rAImv8LT5TYb0BGu6ur4eTcRlOCW1WgGk6PqM7SeVfnCW.lLG1qO'
810
}
911

12+
# print_status: A decorator function that prints the status of the wrapped function execution.
1013
def print_status(func):
1114
def wrapper(*args, **kwargs):
1215
print(f"Executing {func.__name__}...")
@@ -18,6 +21,10 @@ def wrapper(*args, **kwargs):
1821
return result
1922
return wrapper
2023

24+
# Creates a new JSONBin with some initial data ({"empty": "empty"}).
25+
# Uses a session to manage the request.
26+
# Handles exceptions to print an error message and returns None if an error occurs.
27+
# Returns the bin_id from the response if the request is successful.
2128
@print_status
2229
def create_data():
2330
session = requests.Session()
@@ -32,6 +39,10 @@ def create_data():
3239
finally:
3340
session.close()
3441

42+
# Updates the data in the specified JSONBin using its bin_id.
43+
# Constructs the URL for the specific bin.
44+
# Handles exceptions to print an error message and returns False if an error occurs.
45+
# Returns True if the request is successful.
3546
@print_status
3647
def update_data(bin_id, json_data):
3748
session = requests.Session()

0 commit comments

Comments
 (0)