@@ -7553,6 +7553,47 @@ def get_file_data(self, file_name, folder=None):
75537553 folder = constants.Files.DOWNLOADS_FOLDER
75547554 return page_utils._get_file_data(folder, file_name)
75557555
7556+ def print_to_pdf(self, name, folder=None):
7557+ """Saves the current page as a PDF.
7558+ If no folder is specified, uses the folder where pytest was called.
7559+ If the folder provided doesn't exist, it will get created.
7560+ @Params
7561+ name - The name to give the PDF file. Must end in ".pdf".
7562+ folder - The directory where you want to save the PDF."""
7563+ import base64
7564+ from selenium.webdriver.common.print_page_options import PrintOptions
7565+
7566+ if not name.lower().endswith(".pdf"):
7567+ raise Exception('PDF name {%s} must end in ".pdf"!)' % name)
7568+ download_file_lock = fasteners.InterProcessLock(
7569+ constants.MultiBrowser.DOWNLOAD_FILE_LOCK
7570+ )
7571+ if self.__is_cdp_swap_needed():
7572+ with download_file_lock:
7573+ with suppress(Exception):
7574+ shared_utils.make_writable(
7575+ constants.MultiBrowser.DOWNLOAD_FILE_LOCK
7576+ )
7577+ if folder and not os.path.exists(folder):
7578+ os.makedirs(folder)
7579+ self.cdp.print_to_pdf(name, folder)
7580+ return
7581+ self.wait_for_ready_state_complete()
7582+ print_options = PrintOptions()
7583+ pdf_base64 = self.driver.print_page(print_options)
7584+ with download_file_lock:
7585+ with suppress(Exception):
7586+ shared_utils.make_writable(
7587+ constants.MultiBrowser.DOWNLOAD_FILE_LOCK
7588+ )
7589+ if folder and not os.path.exists(folder):
7590+ os.makedirs(folder)
7591+ filename = name
7592+ if folder:
7593+ filename = os.path.join(folder, name)
7594+ with open(filename, "wb") as f:
7595+ f.write(base64.b64decode(pdf_base64))
7596+
75567597 def get_downloads_folder(self):
75577598 """Returns the path of the SeleniumBase "downloaded_files/" folder.
75587599 Calling self.download_file(file_url) will put that file in here.
0 commit comments