def sample42(request): clientId = request.POST.get("clientId") privateKey = request.POST.get("privateKey") fileId = request.POST.get("fileId") basePath = request.POST.get("basePath") iframe = None # Checking clientId, privateKey and fileId if not clientId or not privateKey or not fileId: return render_to_response("__main__:templates/sample42.pt", dict(error="You do not enter all parameters")) ####Create Signer, ApiClient and Storage Api objects # Create signer object signer = GroupDocsRequestSigner(privateKey) # Create apiClient object apiClient = ApiClient(signer) # Create Ant Api object antApi = AntApi(apiClient) # Create Storage Api object storageApi = StorageApi(apiClient) # Create Async api object asyncApi = AsyncApi(apiClient) # Set base Path if not basePath: basePath = "https://api.groupdocs.com/v2.0" antApi.basePath = basePath asyncApi.basePath = basePath storageApi.basePath = basePath try: # Get all annotations from document getAllAnnotations = antApi.ListAnnotations(clientId, fileId) if getAllAnnotations.status == "Ok": # Create list of result document type convertType = ["pdf"] # Create JobInfo object and set attributes jobInfo = JobInfo() jobInfo.actions = "ImportAnnotations" jobInfo.out_formats = convertType jobInfo.status = "-1" jobInfo.email_results = True rand = random.randint(0, 500) jobInfo.name = "test" + str(rand) # Create job createJob = asyncApi.CreateJob(clientId, jobInfo) if createJob.status != "Ok": raise Exception(createJob.error_message) else: # Add document to the job addJobDocument = asyncApi.AddJobDocument(clientId, createJob.result.job_id, fileId, False) if addJobDocument.status != "Ok": raise Exception(addJobDocument.error_message) else: # Change job status jobInfo.status = "0" # Update job with new status updateJob = asyncApi.UpdateJob(clientId, createJob.result.job_id, jobInfo) if updateJob.status != "Ok": raise Exception(updateJob.error_message) time.sleep(5) # Get result file from job by it's ID getJobDocument = asyncApi.GetJobDocuments(clientId, createJob.result.job_id) if getJobDocument.status != "Ok": raise Exception(getJobDocument.error_message) else: # Get document GUID and Name from job fileGuid = getJobDocument.result.inputs[0].outputs[0].guid fileName = getJobDocument.result.inputs[0].outputs[0].name # Obtaining file stream of downloading file and definition of folder where to download file # Clear downloads folder currentDir = os.path.dirname(os.path.realpath(__file__)) if os.path.isdir(currentDir + "/../downloads"): # Get list of files for the_file in os.listdir(currentDir + "/../downloads"): file_path = os.path.join(currentDir + "/../downloads", the_file) try: # Delete file from folder os.unlink(file_path) except Exception, e: print e # Set path to were file will be downloaded newPath = currentDir + "/../downloads/" if not os.path.exists(newPath): os.makedirs(newPath) # Downlaoding of file fs = storageApi.GetFile(clientId, fileGuid) if fs: # Write file from stream to the downloads folder filePath = newPath + fileName with open(filePath, "wb") as fp: shutil.copyfileobj(fs.inputStream, fp) # Generate template message with link by clicking on which user can download file to he's local machine message = ( '<p><span style="color:green">File with annotations was downloaded to server\'s local folder. You can download it from <a href="/download_file?FileName=' + fileName + '">here</a></span></p>' ) # Generation of iframe URL using $pageImage->result->guid # iframe to production server if basePath == "https://api.groupdocs.com/v2.0": iframe = "https://apps.groupdocs.com/document-viewer/embed/" + fileGuid # iframe to dev server elif basePath == "https://dev-api.groupdocs.com/v2.0": iframe = "https://dev-apps.groupdocs.com/document-viewer/embed/" + fileGuid # iframe to test server elif basePath == "https://stage-api.groupdocs.com/v2.0": iframe = "https://stage-apps.groupdocs.com/document-viewer/embed/" + fileGuid elif basePath == "http://realtime-api.groupdocs.com": iframe = "http://realtime-apps.groupdocs.com/document-viewer/embed/" + fileGuid iframe = signer.signUrl(iframe) else: raise Exception("Wrong file ID!") else:
upload = storageApi.UploadWeb(clientId, url) if upload.status == "Ok": #Add GUID of uploaded file to list guidList.append(upload.result.guid) else: raise Exception(upload.error_message) except Exception, e: return render_to_response('__main__:templates/sample33.pt', { 'error' : str(e) }) ####Make a request to Signature API using clientId try: #Create list of result document type convertType = [] convertType.append("pdf") #Create JobInfo object and set attributes jobInfo = JobInfo() jobInfo.actions = "convert, combine" jobInfo.out_formats = convertType jobInfo.status = "-1" jobInfo.email_results = True rand = random.randint(0, 500) jobInfo.name = "test" + str(rand) #Create job createJob = asyncApi.CreateJob(clientId, jobInfo) if createJob.status == "Ok": for guid in guidList: try: #Add all uploaded files to created job addJobDocument = asyncApi.AddJobDocument(clientId, createJob.result.job_id, guid, False) if addJobDocument.status != "Ok": raise Exception(addJobDocument.error_message)
upload = storageApi.UploadWeb(clientId, url) if upload.status == "Ok": #Add GUID of uploaded file to list guidList.append(upload.result.guid) else: raise Exception(upload.error_message) except Exception, e: return render_to_response('__main__:templates/sample33.pt', {'error': str(e)}) ####Make a request to Signature API using clientId try: #Create list of result document type convertType = [] convertType.append("pdf") #Create JobInfo object and set attributes jobInfo = JobInfo() jobInfo.actions = "convert, combine" jobInfo.out_formats = convertType jobInfo.status = "-1" jobInfo.email_results = True rand = random.randint(0, 500) jobInfo.name = "test" + str(rand) #Create job createJob = asyncApi.CreateJob(clientId, jobInfo) if createJob.status == "Ok": for guid in guidList: try: #Add all uploaded files to created job addJobDocument = asyncApi.AddJobDocument( clientId, createJob.result.job_id, guid, False) if addJobDocument.status != "Ok":