Project

General

Profile

Download (1.15 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / calebasse / pdftk.py @ 05a6bb20

1
import os
2
import subprocess
3
import tempfile
4

    
5
class PdfTk(object):
6
    def __init__(self, pdftk_path=None, prefix='tmp'):
7
        self._pdftk_path = pdftk_path
8
        self.prefix = prefix
9

    
10
    @property
11
    def pdftk_path(self):
12
        return self._pdftk_path or '/usr/bin/pdftk'
13

    
14
    def do(self, args, wait=True):
15
        args = [self.pdftk_path] + args
16
        proc = subprocess.Popen(args)
17
        if wait:
18
            return proc.wait()
19
        else:
20
            return proc
21

    
22
    def concat(self, input_files, output_file, wait=True):
23
        args = input_files + ['cat', 'output', output_file, 'compress', 'flatten']
24
        return self.do(args, wait=wait)
25

    
26
    def background(self, input_file, background_file, output_file, wait=True):
27
        args = [input_file, 'background', background_file, 'output', output_file, 'compress']
28
        return self.do(args, wait=wait)
29

    
30

    
31
if __name__ == '__main__':
32
    import sys
33

    
34
    pdftk = PdfTk()
35
    if sys.argv[1] == 'concat':
36
        pdftk.concat(sys.argv[2].split(','), sys.argv[3])
37
    elif sys.argv[1] == 'background':
38
        pdftk.background(*sys.argv[2:5])
39
    else:
40
        raise RuntimeError('Unknown command %s', sys.argv)
(11-11/17)