1
- from __future__ import print_function
2
- import sys
3
- import fitz
4
- #------------------------------------------------------------------------------
5
- # Example program
6
- # License: GNU GPL V3
7
- # Imports a new file into an existing PDF
8
- # Command line:
9
- # python embedded-import.py out.pdf input.file
10
- #------------------------------------------------------------------------------
11
- pdffn = sys .argv [1 ]
12
- impfn = sys .argv [2 ]
13
-
14
- doc = fitz .open (pdffn )
15
-
16
- # to be on the safe side, always open as binary
17
- content = open (impfn , "rb" ).read () # read all file content in
18
-
19
- # now some description of what is going to imported
20
- name = "import1" # mandatory: unique name inside the PDF
21
- filename = impfn # optional filename, need not be the original
22
- desc = "something documentary" # optional other comments
23
-
24
- # import the file into the PDF
25
- doc .embeddedFileAdd (content , name , filename , desc )
26
- # save PDF (either incremental or to new PDF file)
27
- doc .saveIncr ()
1
+ from __future__ import print_function
2
+ import fitz
3
+ import argparse
4
+
5
+ #------------------------------------------------------------------------------
6
+ # Example program
7
+ # License: GNU GPL V3
8
+ # Imports a new file into an existing PDF
9
+ # Command line:
10
+ # python embedded-import.py some.pdf embed.file
11
+ #------------------------------------------------------------------------------
12
+
13
+ parser = argparse .ArgumentParser (description = "Enter PDF, file to embed, and optional name, description and output pdf." )
14
+ parser .add_argument ('pdf' , help = 'PDF filename' )
15
+ parser .add_argument ('file' , help = 'name of embedded file' )
16
+ parser .add_argument ('-n' , "--name" , help = 'name for embedded file entry (default: file)' )
17
+ parser .add_argument ('-d' , "--desc" , help = 'description (default: file)' )
18
+ parser .add_argument ('-o' , "--output" , help = 'output PDF (default: modify pdf)' )
19
+ args = parser .parse_args ()
20
+ delim = args .desc # requested CSV delimiter character
21
+ pdffn = args .pdf
22
+ impfn = args .file
23
+
24
+ doc = fitz .open (pdffn )
25
+ if not args .name :
26
+ name = impfn
27
+ desc = args .desc
28
+ if not args .desc :
29
+ desc = impfn
30
+
31
+ # to be on the safe side, always open as binary
32
+ content = open (impfn , "rb" ).read () # read all file content in
33
+
34
+ # import the file into the PDF
35
+ doc .embeddedFileAdd (content , name , impfn , desc )
36
+ # save PDF (either incremental or to new PDF file)
37
+ if not args .output :
38
+ doc .saveIncr ()
39
+ else :
40
+ doc .save (args .output , garbage = 4 , deflate = True )
0 commit comments