def convert_to_pdf(user, presentationObject): inputFile = "%s/%s" % (settings.CURRENT_DIR, presentationObject.presentation_slides) profile = user.profile # Location to store user's files # based on the hash of their username levelOne = profile.user_hash[0:4] levelTwo = profile.user_hash[4:8] path = "%s/%s/%s" % (settings.STORAGE_ROOT, levelOne, levelTwo) url_path = "%s/%s/%s" % (settings.STORAGE_URL, levelOne, levelTwo) # Check if directory exists, if not...create it if not os.path.exists(path): os.makedirs(path) # Check that the uploaded file exists if os.path.exists("%s" % (inputFile)): print "Uploaded file exists" # BaseName of the uploaded file baseName = os.path.basename(inputFile) # Name of the PDF File pdfFile = "%s/%s.pdf" % (path, baseName) pdfFile_url = "%s/%s.pdf" % (url_path, baseName) # New file location newFile = "%s/%s" % (path, baseName) newFile_url = "%s/%s" % (url_path, baseName) # Check to see if the file exists in the user storage if os.path.exists("%s" % (newFile)): if filecmp.cmp(inputFile, newFile): print "upload file and newFile the same, moving on" # Files are the same, no need to move pass else: # Move uploaded file into user's file location print "Moving uploaded file %s into location %s" % (inputFile, path) shutil.move(inputFile, path) else: # Move uploaded file into user's file location print "File does not exist in storage location" print "Moving uploaded file %s into location %s" % (inputFile, path) shutil.move(inputFile, path) # Convert presentation to PDF try: converter = DocumentConverter() converter.convert(newFile, pdfFile) except DocumentConversionException, exception: print "ERROR! " + str(exception) exit(1) except ErrorCodeIOException, exception: print e, exception print "ERROR! ErrorCodeIOException %d" % exception.ErrCode exit(1)
def main(fn_template, fn_data): fp_data = open(fn_data) reader = csv.DictReader(open(fn_data)) users = list(reader) converter = DocumentConverter() for user in users: user["filename"] = "%s_%s.odt" % (user["first_name"], user["last_name"]) user["filename_pdf"] = "%s_%s.pdf" % (user["first_name"], user["last_name"]) mail_merge(fn_template, user) converter.convert(user["filename"], user["filename_pdf"])
if not isdir(input_folder): print 'no such input folder: %s' % input_folder exit(1) if not isdir(output_folder): print 'no such output folder: %s' % output_folder exit(1) for entry in listdir(input_folder): fpath = input_folder + '/' + entry if isfile(fpath): fname = splitext(entry)[0] #print 'found file %s with name %s' % (entry, fname) new_fpath = output_folder + '/' + fname if not isdir(new_fpath): mkdir(new_fpath) try: converter = DocumentConverter() converter.convert(fpath, new_fpath + ('/%s.html' % fname)) converter.convert(fpath, new_fpath + ('/%s.pdf' % fname)) convert_file(new_fpath, fname + '.html') except DocumentConversionException, exception: print "ERROR!" + str(exception) exit(1) except Exception, exception: print "ERROR! ErrorCodeIOException %s" % exception exit(1) #else: # print 'found non-file %s' % entry
docextension=docextension[1:] # Remove leading period if not docextension.lower() in allowedextensions: print "Unknown extension '%s'" % docextension.lower() print "Only %s are allowed" % ", ".join(allowedextensions) sys.exit(1) # Clean up any old files for f in ('%s/converted.wiki' % tempdir, '%s/converted.xml' % tempdir): if os.path.exists(f): os.unlink(f) # Convert the actual document. This will always create temporary files, # there is no way to get the data in a buffer. with IOWrapper("Converting document format..."): converter = DocumentConverter() converter.convert(inputfile, '%s/converted.wiki' % tempdir) converter.convert(inputfile, '%s/converted.xml' % tempdir) # Read the converted wiki format, and append our hardcoded additions wf = codecs.open('%s/converted.wiki' % tempdir, "r", "utf-8") wikilines = wf.readlines() wf.close() wikilines.append("\n[[Category:word2mediawiki]]\n") # Grab images out of the XML data parser = XMLParser(target=ImageGrabber()) parser.feed(open('%s/converted.xml' % tempdir).read()) parser.close() # Prepare for uploading to mediawiki # Make sure we are logged in
if not isdir(input_folder): print "no such input folder: %s" % input_folder exit(1) if not isdir(output_folder): print "no such output folder: %s" % output_folder exit(1) for entry in listdir(input_folder): fpath = input_folder + "/" + entry if isfile(fpath): fname = splitext(entry)[0] # print 'found file %s with name %s' % (entry, fname) new_fpath = output_folder + "/" + fname if not isdir(new_fpath): mkdir(new_fpath) try: converter = DocumentConverter() converter.convert(fpath, new_fpath + ("/%s.html" % fname)) converter.convert(fpath, new_fpath + ("/%s.pdf" % fname)) convert_file(new_fpath, fname + ".html") except DocumentConversionException, exception: print "ERROR!" + str(exception) exit(1) except Exception, exception: print "ERROR! ErrorCodeIOException %s" % exception exit(1) # else: # print 'found non-file %s' % entry
docextension=docextension[1:] # Remove leading period if not docextension.lower() in allowedextensions: print "Unknown extension '%s'" % docextension.lower() print "Only %s are allowed" % ", ".join(allowedextensions) sys.exit(1) # Clean up any old files for f in ('%s/converted.wiki' % tempdir, '%s/converted.xml' % tempdir): if os.path.exists(f): os.unlink(f) # Convert the actual document. This will always create temporary files, # there is no way to get the data in a buffer. with IOWrapper("Converting document format..."): converter = DocumentConverter() converter.convert(inputfile, '%s/converted.wiki' % tempdir) converter.convert(inputfile, '%s/converted.xml' % tempdir) # Read the converted wiki format, and append our hardcoded additions wf = codecs.open('%s/converted.wiki' % tempdir, "r", "utf-8") wikilines = wf.readlines() wf.close() wikilines.append("\n[[Category:word2mediawiki]]\n") # Grab images out of the XML data parser = XMLParser(target=ImageGrabber()) # XXX: rewrite the namespace so as not to confuse certain XML parsers # (changes either in newer open/libreoffice or newer XML parsers require this) parser.feed(open('%s/converted.xml' % tempdir).read().replace("\"http://www.w3.org/XML/1998/namespace\"","\"http://www.w3.org/XML/1998/namespace_bugfix\"")) parser.close()
def run(self): while True: doc = model.get_task_to_process() if doc is None: time.sleep(1) else: try: #log selected document web.debug(doc) #ensure that tmp direcory exist if not os.path.exists('/tmp/converterdir'): os.makedirs('/tmp/converterdir') #check if document need to been converted interim_path = '/tmp/converterdir/test2.pdf' if re.search(r"\.pdf$", doc.srcname, re.IGNORECASE | re.MULTILINE) is None: converter = DocumentConverter() converter.convert(doc.srcname, interim_path) #wait for converter closes time.sleep(1) else: #call pdftk to atach original pdf to new pdf without signature #subprocess.call("/opt/zimbra/bin/pdftk "+ doc.srcname +" cat '"+doc.srcname +"' output "+interim_path, shell=True, stdout=self.flog) interim_path = doc.srcname web.debug(interim_path) #call pdftk to atach original subprocess.call("/opt/zimbra/bin/pdftk " + interim_path + " attach_files '" + doc.srcname + "' output /tmp/converterdir/test3.pdf", shell=True, stdout=self.flog) #sign document with Private Key from key.properties d = datetime.today() unic_file_name = d.strftime('%m_%d_%H_%M_%S') subprocess.call( "java -jar /opt/zimbra/bin/signapp/signApp.jar /tmp/converterdir/test3.pdf /tmp/converterdir/" + unic_file_name + ".pdf /opt/zimbra/bin/signapp/key.properties", shell=True, stdout=self.flog) #mark document as ready model.update_task( doc.idtask, 10, '/tmp/converterdir/' + unic_file_name + '.pdf') except DocumentConversionException, exception: model.update_task(doc.idtask, -1) web.debug(datetime.today().strftime('%y-%m-%d %H:%M:%S') + "ERROR! " + str(exception)) except ErrorCodeIOException, exception: model.update_task(doc.idtask, -1) web.debug(datetime.today().strftime('%y-%m-%d %H:%M:%S') + "ERROR! ErrorCodeIOException %d" % exception.ErrCode) except Exception, exception: model.update_task(doc.idtask, -1) web.debug(datetime.today().strftime('%y-%m-%d %H:%M:%S') + " unexpected error! - " + str(exception))