def ODTconvert(self, odtfile, destformat, _SOAPContext=None): """ Convert an ODT File to a PDF (etc) without doing any find and replace stuff. @note: This method fills a need for the company I work for. Kind of a stepping stone to be able to implement the rest of this thing. @note: This is probably a HUGE security hole... what with imbedded macros, etc. Then the next step would be to do the find/replace stuff on this, which could lead to XML attacks, etc. @param odtfile: base64 encoded ODT file to convert @param destformat: format of the file the odtfile should be converted to @return: str base64 encoded string containing the converted file """ self._logSoapInfo(_SOAPContext) try: # write the input file destodt = self._getTempFile(".odt") outfile = codecs.open(destodt, "w") outfile.write(base64.b64decode(odtfile)) outfile.close() # get a filename for the outfile destpdf = self._getTempFile(".%s" % destformat) # convert file OpenOfficeDocument.convert(destodt, destpdf) # read converted file infile = codecs.open(destpdf, "r") pdf = base64.b64encode(infile.read()) infile.close() self.logging.info("in-file: %s - out-file: %s - format: %s" % (destodt, destpdf, destformat)) except: self.logging.error( "Error converting file to %s ------ in-file: %s out-file: %s" % (destformat, destodt, destpdf) ) return "error: converting file to %s" % destformat try: os.unlink(destodt) except: self.logging.warning("could not remove files %s" % destodt) try: os.unlink(destpdf) except: self.logging.warning("could not remove files %s" % destpdf) return pdf
def _doConversion(self, param0, param1, param2): # if not os.path.exists( param0 ): # return "error: templatefile: %s does not exist" % param0 if self._getFileExtension(param0) == "doc": odtName = self._getTempFile(".odt") # self.converter.convert( param0, odtName ) OpenOfficeDocument.convert(param0, odtName) else: odtName = param0 """this has been moved here because it's more efficient then running multiple times, and does not cause caching problems across multiple soap requests. Side note: Man I love unit tests""" skipMerge = False # check to see if param1 is an XML file, if so parse it into an list or whatever params = None if type(param1).__name__ in ("str", "unicode"): if param1[0:5] == "<?xml": params = self._parseXMLParams(param1) params = self._sortparams(params) if params is None: try: params = param1["item"] # no idea why this stuff is inside of 'item'... params = self._sortparams(params) except: """if there is only a single value passed it will come out as a struct instead of a list""" try: params = [{}] for k, x in param0: params[0] = dict() params[0][k] = [] for y in x: params[0][k].append(y) except: # can't do merge, bad data given return "error: cant do merge, bad data given" self.logging.error("cant do merge, bad data given") return None doctype = param2 """http://docs.python.org/library/shutil.html""" sourcezip = zipfile.ZipFile(u"docs/" + odtName, "r") # create destination zip destodt = self._getTempFile(".odt") destzip = zipfile.ZipFile(destodt, "w") # copy all file from the source zip(odt) the the destination zip tmpfiles = {} xmlFileList = ["content.xml", "meta.xml", "styles.xml"] """Write all of the contents of the sourcezip to the destzip if the the filename is not in the xmlFileList list""" for x in sourcezip.namelist(): if x not in xmlFileList: tmp = sourcezip.read(x) destzip.writestr(x, tmp) """these files need to be done last, in case there is any modifiers that want to change the contents of the zip, for example the image| modifier""" for x in xmlFileList: tmp = sourcezip.read(x) tmp = self._replaceContent(sourcezip.read(x), params, destzip) """I could not get the contents of the xml (tmp) file to write directly to the zip because of file encoding stuff with french characters, so I'm writing the contents to a utf-8 file, then putting that file into the zip archive.""" tmpFileName = "%s" % self._getTempFile() tmpFile = codecs.open(tmpFileName, "w", "utf-8") try: tmpFile.write(unicode(tmp, "utf-8")) # for english except: tmpFile.write(tmp) # for french tmpFile.close() destzip.write(tmpFileName, x) # remove file now os.unlink(tmpFileName) # clean up destzip.close() sourcezip.close() # now convert the odt to pdf destpdf = u"%s" % (self._getTempFile("." + doctype)) self.logging.info("about to convert %s to %s" % (destodt, destpdf)) try: # this is now going to create an instance of converter on demand in case OpenOffice # crashes and needs to restart. OpenOfficeDocument.convert(destodt, destpdf) self.logging.info("converted %s to %s" % (destodt, destpdf)) # os.unlink( destodt ) except: errormsg = "Could not convert document, usually bad xml. Check ODT File: '%s'" % destodt self.logging.error(errormsg) return "error: %s" % errormsg return destpdf