#!/usr/bin/env python # author: Patrick Carroll # This file is part of ooo2pdf. # ooo2pdf is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # ooo2pdf is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with ooo2pdf. If not, see . import uno, os, sys, tempfile from com.sun.star.beans import PropertyValue def get_context(): localContext = uno.getComponentContext() resolver = localContext.ServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", localContext) ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext") return ctx def check_args(args): if len(args) > 3 or (len(args) == 2 and (args[1] == '-h' or args[1] == '--help')): print >>sys.stderr, """Usage: %s [input-file] [output-file] %s [-h|--help] If input-file is not specified input will be read from standard input. Also, if output-file is not specified, output will be written to standard output. """ % (args[0], args[0]) sys.exit(1) return args def parse_args(args): input, output, from_stdin, to_stdout = None, None, True, True if len(args) > 1: input = args[1] from_stdin = False if len(args) > 2: output = args[2] to_stdout = False else: output = tempfile.mktemp() else: output = tempfile.mktemp() input = tempfile.mktemp() fh = open(input, 'w') while 1: try: chunk = sys.stdin.read(8192) if chunk == '': break fh.write(chunk) except KeyboardInterrupt: fh.close() os.remove(input) sys.exit(0) fh.close() return os.path.abspath(input), os.path.abspath(output), \ from_stdin, to_stdout def cleanup_files(input, output, from_stdin, to_stdout): if from_stdin: os.remove(input) if to_stdout: fh = open(output, "r") while 1: chunk = fh.read(8192) if chunk == '': break; sys.stdout.write(chunk) fh.close() # this was only a tmp os.remove(output) if __name__ == "__main__": input, output, from_stdin, to_stdout = parse_args(check_args(sys.argv)) ctx = get_context() desktop = ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx) doc = desktop.loadComponentFromURL('file://' + input, '_blank', 0, ()) if not doc: print >>sys.stderr, "There was a problem communicating with OOo which is specific to this document format" else: pdf_filters = [ 'writer_pdf_Export', 'writer_web_pdf_Export', 'writer_globaldocument_pdf_Export', 'calc_pdf_Export', 'impress_pdf_Export', 'draw_pdf_Export', 'math_pdf_Export' ] for filter in pdf_filters: try: p = PropertyValue() p.Name, p.Value = 'FilterName', filter doc.storeToURL('file://' + output, tuple([ p ])) break except: pass doc.dispose() ctx.ServiceManager cleanup_files(input, output, from_stdin, to_stdout)