@@ -749,6 +749,7 @@ def invoke(self,
749749 filename_rule = 'Pid' ,
750750 dir_rule = None ,
751751 delete_original_file = False ,
752+ encrypt = None ,
752753 ** kwargs ,
753754 ):
754755 if photo is None and album is None :
@@ -766,14 +767,14 @@ def invoke(self,
766767 pdf_filepath = self .decide_filepath (album , photo , filename_rule , 'pdf' , pdf_dir , dir_rule )
767768
768769 # 调用 img2pdf 把 photo_dir 下的所有图片转为pdf
769- img_path_ls , img_dir_ls = self .write_img_2_pdf (pdf_filepath , album , photo )
770+ img_path_ls , img_dir_ls = self .write_img_2_pdf (pdf_filepath , album , photo , encrypt )
770771 self .log (f'Convert Successfully: JM{ album or photo } → { pdf_filepath } ' )
771772
772773 # 执行删除
773774 img_path_ls += img_dir_ls
774775 self .execute_deletion (img_path_ls )
775776
776- def write_img_2_pdf (self , pdf_filepath , album : JmAlbumDetail , photo : JmPhotoDetail ):
777+ def write_img_2_pdf (self , pdf_filepath , album : JmAlbumDetail , photo : JmPhotoDetail , encrypt ):
777778 import img2pdf
778779
779780 if album is None :
@@ -795,8 +796,22 @@ def write_img_2_pdf(self, pdf_filepath, album: JmAlbumDetail, photo: JmPhotoDeta
795796 with open (pdf_filepath , 'wb' ) as f :
796797 f .write (img2pdf .convert (img_path_ls ))
797798
799+ if encrypt :
800+ self .encrypt_pdf (pdf_filepath , encrypt )
801+
798802 return img_path_ls , img_dir_ls
799803
804+ def encrypt_pdf (self , pdf_filepath : str , encrypt : dict ):
805+ try :
806+ import pikepdf
807+ except ImportError :
808+ self .warning_lib_not_install ('pikepdf' )
809+ return
810+
811+ password = str (encrypt .get ('password' , '' ))
812+ with pikepdf .open (pdf_filepath , allow_overwriting_input = True ) as pdf :
813+ pdf .save (pdf_filepath , encryption = pikepdf .Encryption (user = password , owner = password ))
814+
800815
801816class LongImgPlugin (JmOptionPlugin ):
802817 plugin_key = 'long_img'
0 commit comments