1- import os
1+ import os , shutil
22import sys
33import pdfkit
44import base64
1212from PyQt5 .QtWidgets import (QApplication , QWidget , QVBoxLayout , QLineEdit ,
1313 QPushButton , QLabel , QMessageBox , QFileDialog )
1414
15+ # Check which platform the program is being executed on
1516if sys .platform == "win32" :
1617 PATH_TO_WKHTMLTOPDF = r'./wkhtmltopdf/bin/wkhtmltopdf.exe'
1718elif sys .platform in ["linux" , "linux2" ]:
1819 PATH_TO_WKHTMLTOPDF = r'./wkhtmltopdf/wkhtmltopdf'
1920elif 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
2224CONFIG = 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
2330OPT = {
2431 'margin-top' : '2in' ,
2532 'margin-bottom' : '1in' ,
2936}
3037TEMP_PDF_PATH = "./PDF/temp.pdf"
3138FINAL_PDF_PATH = "./PDF/output.pdf"
32- BIN_ID = create_data ()
3339QR_FOLDER_PATH = "./QR"
3440UID_FOLDER_PATH = os .path .join (QR_FOLDER_PATH , BIN_ID )
3541OUTPUT_FOLDER_PATH = "./PDF"
3642AR_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
3948os .makedirs (UID_FOLDER_PATH , exist_ok = True )
4049os .makedirs (OUTPUT_FOLDER_PATH , exist_ok = True )
4150
51+ # Returns the number of pages in a PDF file.
4252def 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.
4758def 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.
5668def 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.
92102def 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.
124132def 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.
133143def 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
140151class PDFGeneratorApp (QWidget ):
141152 def __init__ (self ):
142153 super ().__init__ ()
0 commit comments