def execute(self, execution_context: ExecutionContext): """ Executes this operation synchronously using the supplied context and returns a new FileRef instance for the resulting Zip file. The resulting file may be stored in the system temporary directory. See :class:`adobe.pdfservices.operation.io.file_ref.FileRef` for how temporary resources are cleaned up. :param execution_context: The context in which the operation will be executed. :type execution_context: ExecutionContext :return: The FileRef to the result. :rtype: FileRef :raises ServiceApiException: if an API call results in an error response. """ try: self._validate_invocation_count() self._validate(execution_context=execution_context) self._logger.info( "All validations successfully done. Beginning ExtractPDF operation execution" ) location = ExtractPDFAPI.extract_pdf(execution_context, self._source_file_ref, self._extract_pdf_options) self._is_invoked = True file_location = get_temporary_destination_path( target_extension=ExtensionMediaTypeMapping.ZIP.extension) ExtractPDFAPI.download_and_save(location=location, context=execution_context, file_location=file_location) self._logger.info( "Extract Operation Successful - Transaction ID: %s", get_transaction_id(location)) return FileRef.create_from_local_file(file_location) except OperationException as oex: raise ServiceApiException( message=oex.error_message, error_code=oex.error_code, request_tracking_id=oex.request_tracking_id, status_code=oex.status_code)
try: # get base path. base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Initial setup, create credentials instance. credentials = Credentials.service_account_credentials_builder() \ .from_file(base_path + "/pdftools-api-credentials.json") \ .build() # Create an ExecutionContext using credentials and create a new operation instance. execution_context = ExecutionContext.create(credentials) extract_pdf_operation = ExtractPDFOperation.create_new() # Set operation input from a source file. source = FileRef.create_from_local_file(base_path + "/resources/extractPdfInput.pdf") extract_pdf_operation.set_input(source) # Build ExtractPDF options and set them into the operation extract_pdf_options: ExtractPDFOptions = ExtractPDFOptions.builder() \ .with_elements_to_extract([PDFElementType.TEXT, PDFElementType.TABLES]) \ .with_element_to_extract_renditions(PDFElementType.TABLES) \ .with_table_structure_format(TableStructureType.CSV) \ .build() extract_pdf_operation.set_options(extract_pdf_options) # Execute the operation. result: FileRef = extract_pdf_operation.execute(execution_context) # Save the result to the specified location. result.save_as(base_path + "/output/ExtractTextTableWithTableStructure.zip")