summaryrefslogtreecommitdiff
path: root/sandbox/docbook/scripts/fop.py
blob: 71ed35e08783b9ed86cb3820ae55b0b75fac0b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import sys, os
from subprocess import Popen, PIPE


class NotValidFoExeption(Exception):
    pass

class Fop():

    def __init__(self):
        fop_home = os.environ.get('FOP_HOME')
        if not fop_home:
            raise OSError('You must set the "FOP_HOME" variable')
        # plat = os.uname()
        # plat = platform.system()
        plat = os.name
        if  plat == 'posix':
            fop_bin = os.path.join(fop_home, 'fop')
        else:
            fop_bin = os.path.join(fop_home, 'fop.bat')
        self.fop_bin = fop_bin

    def to_pdf(self, fo_file, out_file = None):
        if not out_file:
            filename, ext = os.path.splitext(fo_file)
            out_file = '{0}.pdf'.format(filename)
        command_list = [self.fop_bin, '-fo', fo_file, '-pdf', out_file]
        p = Popen(command_list, stdout=PIPE, stderr=PIPE)
        stdout, stderr = p.communicate()
        if p.returncode:
            print(stderr)
        # on windows check for existence of pdf file

if __name__ == '__main__':
    fop_obj = Fop()
    fop_obj.to_pdf(sys.argv[1])