epic-s6ts
[kivitendo-erp.git] / scripts / oo-uno-convert-pdf.py
1 #!/usr/bin/python
2
3 import uno, sys
4
5 from unohelper import Base, systemPathToFileUrl, absolutize
6 from os import getcwd
7 from os.path import splitext
8 from com.sun.star.beans import PropertyValue
9
10 def establish_connection(port):
11   local_ctx = uno.getComponentContext()
12   local_smgr = local_ctx.ServiceManager
13
14   resolver = local_smgr.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local_ctx)
15   ctx = resolver.resolve("uno:socket,host=localhost,port=%s;urp;StarOffice.ComponentContext" % port)
16   smgr = ctx.ServiceManager
17
18   desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
19
20   return desktop
21
22 def load_file(desktop, path):
23   cwd = systemPathToFileUrl(getcwd())
24   file_url = absolutize(cwd, systemPathToFileUrl(path))
25   sys.stderr.write(file_url + "\n")
26
27   in_props = (
28     #   PropertyValue("Hidden", 0 , True, 0),
29     )
30   return desktop.loadComponentFromURL(file_url, "_blank", 0, in_props)
31
32
33 def write_pdf(doc, path):
34   out_props = (
35     PropertyValue("FilterName", 0, "writer_pdf_Export", 0),
36     PropertyValue("Overwrite", 0, True, 0),
37     )
38
39   (dest, ext) = splitext(path)
40   dest = dest + ".pdf"
41   dest_url = absolutize(systemPathToFileUrl(getcwd()), systemPathToFileUrl(dest))
42   sys.stderr.write(dest_url + "\n")
43   doc.storeToURL(dest_url, out_props)
44   doc.dispose()
45
46 def main():
47   if len(sys.argv) <= 2:
48     sys.exit(1)
49
50   try:
51     desktop = establish_connection(sys.argv[1])
52   except:
53     sys.exit(2)
54
55   try:
56     doc = load_file(desktop, sys.argv[2])
57     if not doc:
58       sys.exit(3)
59   except:
60     sys.exit(3)
61
62   try:
63     write_pdf(doc, sys.argv[2])
64   except:
65     sys.exit(4)
66
67   sys.exit(0)
68
69 main()