71
71
from .constants import CatalogAttributes as CA
72
72
from .constants import (
73
73
CatalogDictionary ,
74
- FileSpecificationDictionaryEntries ,
75
74
GoToActionArguments ,
76
75
ImageType ,
77
76
InteractiveFormDictEntries ,
95
94
DecodedStreamObject ,
96
95
Destination ,
97
96
DictionaryObject ,
97
+ EmbeddedFile ,
98
98
Fit ,
99
99
FloatObject ,
100
100
IndirectObject ,
@@ -741,7 +741,7 @@ def add_js(self, javascript: str) -> None:
741
741
)
742
742
js_list .append (self ._add_object (js ))
743
743
744
- def add_attachment (self , filename : str , data : Union [str , bytes ]) -> None :
744
+ def add_attachment (self , filename : str , data : Union [str , bytes ]) -> "EmbeddedFile" :
745
745
"""
746
746
Embed a file inside the PDF.
747
747
@@ -753,85 +753,11 @@ def add_attachment(self, filename: str, data: Union[str, bytes]) -> None:
753
753
filename: The filename to display.
754
754
data: The data in the file.
755
755
756
- """
757
- # We need three entries:
758
- # * The file's data
759
- # * The /Filespec entry
760
- # * The file's name, which goes in the Catalog
761
-
762
- # The entry for the file
763
- # Sample:
764
- # 8 0 obj
765
- # <<
766
- # /Length 12
767
- # /Type /EmbeddedFile
768
- # >>
769
- # stream
770
- # Hello world!
771
- # endstream
772
- # endobj
773
-
774
- if isinstance (data , str ):
775
- data = data .encode ("latin-1" )
776
- file_entry = DecodedStreamObject ()
777
- file_entry .set_data (data )
778
- file_entry .update ({NameObject (PA .TYPE ): NameObject ("/EmbeddedFile" )})
779
-
780
- # The Filespec entry
781
- # Sample:
782
- # 7 0 obj
783
- # <<
784
- # /Type /Filespec
785
- # /F (hello.txt)
786
- # /EF << /F 8 0 R >>
787
- # >>
788
- # endobj
789
-
790
- ef_entry = DictionaryObject ()
791
- ef_entry .update ({NameObject ("/F" ): self ._add_object (file_entry )})
792
-
793
- filespec = DictionaryObject ()
794
- filespec .update (
795
- {
796
- NameObject (PA .TYPE ): NameObject ("/Filespec" ),
797
- NameObject (FileSpecificationDictionaryEntries .F ): create_string_object (
798
- filename
799
- ), # Perhaps also try TextStringObject
800
- NameObject (FileSpecificationDictionaryEntries .EF ): ef_entry ,
801
- }
802
- )
756
+ Returns:
757
+ EmbeddedFile instance for the newly created embedded file.
803
758
804
- # Then create the entry for the root, as it needs
805
- # a reference to the Filespec
806
- # Sample:
807
- # 1 0 obj
808
- # <<
809
- # /Type /Catalog
810
- # /Outlines 2 0 R
811
- # /Pages 3 0 R
812
- # /Names << /EmbeddedFiles << /Names [(hello.txt) 7 0 R] >> >>
813
- # >>
814
- # endobj
815
-
816
- if CA .NAMES not in self ._root_object :
817
- self ._root_object [NameObject (CA .NAMES )] = self ._add_object (
818
- DictionaryObject ()
819
- )
820
- if "/EmbeddedFiles" not in cast (DictionaryObject , self ._root_object [CA .NAMES ]):
821
- embedded_files_names_dictionary = DictionaryObject (
822
- {NameObject (CA .NAMES ): ArrayObject ()}
823
- )
824
- cast (DictionaryObject , self ._root_object [CA .NAMES ])[
825
- NameObject ("/EmbeddedFiles" )
826
- ] = self ._add_object (embedded_files_names_dictionary )
827
- else :
828
- embedded_files_names_dictionary = cast (
829
- DictionaryObject ,
830
- cast (DictionaryObject , self ._root_object [CA .NAMES ])["/EmbeddedFiles" ],
831
- )
832
- cast (ArrayObject , embedded_files_names_dictionary [CA .NAMES ]).extend (
833
- [create_string_object (filename ), filespec ]
834
- )
759
+ """
760
+ return EmbeddedFile ._create_new (self , filename , data )
835
761
836
762
def append_pages_from_reader (
837
763
self ,
0 commit comments