we have seen that pdfkit and other different libraries arent supporting streamlit website to generate pdf as there is also one thing that i dont want to create pdf i want to generate automated pdf of my webpage when i click on button so i have used selenium to generate automated pdf of my webpage here is the code
code
from selenium import webdriver
import json
export_as_pdf = st.button(“Export Report”)
print(st)
if export_as_pdf:
chrome_options = webdriver.ChromeOptions()
settings = {
“recentDestinations”: [{
“id”: “Save as PDF”,
“origin”: “local”,
“account”: “”,
}],
“selectedDestinationId”: “Save as PDF”,
“version”: 2,
## automated formatting of pdf you can change accordingly
“isHeaderFooterEnabled”: False,
“isLandscapeEnabled”: True,
“scalingType”: 3,
“scaling”: “141”,
“Margins”: “Minimum”
}
prefs = {‘printing.print_preview_sticky_settings.appState’: json.dumps(settings)}
chrome_options.add_experimental_option(‘prefs’, prefs)
chrome_options.add_argument(‘–kiosk-printing’)
## that is my chrome driver path you can install it and give yours
CHROMEDRIVER_PATH = r"C:\Users\AI Engineer\chromedriver"
driver = webdriver.Chrome(chrome_options=chrome_options,
executable_path=CHROMEDRIVER_PATH)
driver.get(“your website link”)
#i used time.sleep as my website was taking time to load so after getting url i letted it to
load then after it loads properly selenium will generate its pdf
time.sleep(130)
driver.execute_script(‘window.print();’)
thanks hope this hack works
you will find your pdf in downloads folder in your computer