@@ -424,6 +424,7 @@ def process_pdf(self):
424424
425425 def _process_selection (self , page_input ):
426426 """Process PDF in selection mode."""
427+ self ._ensure_output_path ()
427428 try :
428429 page_numbers = parse_page_ranges (page_input )
429430 except (ValueError , AttributeError ) as e :
@@ -438,6 +439,7 @@ def _process_selection(self, page_input):
438439
439440 def _process_split (self , chunk_input ):
440441 """Process PDF in split mode."""
442+ self ._ensure_output_path ()
441443 try :
442444 chunk_size = int (chunk_input )
443445 if chunk_size <= 0 :
@@ -476,6 +478,21 @@ def _show_success(self, message):
476478 self .status_label .setStyleSheet ("color: #4caf50;" )
477479 QMessageBox .information (self , "Success" , message )
478480
481+ def _ensure_output_path (self ):
482+ """Validate the output path before writing files."""
483+ if not self .output_path :
484+ raise ValueError ("Please choose an output location before processing." )
485+
486+ if not self .input_path :
487+ return
488+
489+ input_path = Path (self .input_path ).resolve ()
490+ output_path = Path (self .output_path ).resolve ()
491+
492+ # Prevent overwriting the source file, which can corrupt the PDF while reading.
493+ if input_path == output_path :
494+ raise ValueError ("Output file must be different from the input file." )
495+
479496
480497if __name__ == "__main__" :
481498 app = QApplication (sys .argv )
0 commit comments