Project

General

Profile

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

calebasse / calebasse / pdftk.py @ 99a62b84

1
import os
2
import subprocess
3

    
4
from calebasse import settings
5

    
6
class PdfTk(object):
7
    def __init__(self, prefix='tmp'):
8
        self.pdftk_path = settings.PDFTK_PATH
9
        self.prefix = prefix
10

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

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

    
23
    def background(self, input_file, background_file, output_file, wait=True):
24
        args = [input_file, 'background', background_file, 'output', output_file, 'compress']
25
        return self.do(args, wait=wait)
26

    
27

    
28
if __name__ == '__main__':
29
    import sys
30

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